Skip to content

Commit

Permalink
test: reset global state between each test and mock bg-handler path
Browse files Browse the repository at this point in the history
  • Loading branch information
IlCallo committed Apr 7, 2022
1 parent fb7d125 commit fd3bd8a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/responsive-image-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@ let generationStatus: 'ready' | 'processing' | 'completed' = 'ready';
let releaseWhenGenerationCompletedTimeout: NodeJS.Timeout;
let generationCompleted: Promise<void>;

function getBgHandlerPath() {
// Short circuit the path since it's not available at test time,
// since we don't run an intermediate build step
if (process.env.NODE_ENV === 'test') {
return '';
}

return require.resolve(join(__dirname, 'bg-handler'));
}

class ResponsiveImagePlugin {
public static loader = require.resolve(
join(__dirname, 'responsive-image-loader'),
);
public static bgHandler = require.resolve(join(__dirname, 'bg-handler'));
public static bgHandler = getBgHandlerPath();

// Shared with the loader
public options: ResponsiveImagePluginConfig;
Expand Down
13 changes: 12 additions & 1 deletion test/responsive-image-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { describe, expect, it } from '@jest/globals';
import { beforeEach, describe, expect, it } from '@jest/globals';
import { writeFileSync } from 'fs';
import { join, resolve } from 'path';
import {
IMG_TAG_PLACEHOLDER_PATTERN,
metadataCache,
urlReplaceMap,
URL_PLACEHOLDER_PATTERN,
} from 'src/parsing';
import { existsOrCreateDirectory } from '../src/base';
Expand Down Expand Up @@ -47,6 +49,15 @@ describe('Responsive image loader', () => {
});

describe('Plain images', () => {
beforeEach(() => {
// Cleans the urlReplaceMap and metadataCache before each test,
// otherwise metadata generation would be skipped or old hashed URIs could be used
for (const key in urlReplaceMap) {
delete urlReplaceMap[key];
}
metadataCache.clear();
});

describe('conversion disabled', () => {
const conversionDisabled = { conversion: { converter: null } };

Expand Down

0 comments on commit fd3bd8a

Please sign in to comment.