Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test-runner-module-mocking: bug when loaded normally in previous test before intercepted in next #2700

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect } from '../chai.js';

// this module gets improted here first and gets cached by the dev server
// resulting in `import { getCurrentHour } from './time-library.js';` being cached too
// the following test intercepts "time-library.js", but that has no impact on the cached "getTimeOfDay.js"
// so the mocking won't work for any module we need to intercept inside "getTimeOfDay.js" in any following test
import { getTimeOfDay } from './fixture/getTimeOfDay.js';

describe('1st test', () => {
it('works', () => {
const timeOfDay = getTimeOfDay();
expect(typeof timeOfDay === 'string').to.equal(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect } from '../chai.js';
import { importMockable } from '../../../browser/index.js';

const path = new URL(import.meta.resolve('./fixture/time-library.js')).pathname;
const timeLibrary = await importMockable(path);
const { getTimeOfDay } = await import('./fixture/getTimeOfDay.js');

let backup;
describe('2nd test', () => {
it('can run a module normally', () => {
backup = timeLibrary.getCurrentHour;
const timeOfDay = getTimeOfDay();
expect(timeOfDay).to.equal('night');
});

it('can intercept a module', () => {
timeLibrary.getCurrentHour = () => 12;
const timeOfDay = getTimeOfDay();
expect(timeOfDay).to.equal('day');
});

it('can restore an intercepted module', () => {
timeLibrary.getCurrentHour = backup;
const timeOfDay = getTimeOfDay();
expect(timeOfDay).to.equal('night');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getCurrentHour } from './time-library.js';

export function getTimeOfDay() {
const hour = getCurrentHour();
if (hour < 6 || hour > 18) {
return 'night';
}
return 'day';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getCurrentHour() {
return 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ describe('moduleMockingPlugin', function test() {
});
});

it('can intercept server relative modules after previous test loaded them normally', async () => {
await runTests({
files: [
path.join(dirname, 'fixtures', 'bug-cache', 'browser-test-1.js'),
path.join(dirname, 'fixtures', 'bug-cache', 'browser-test-2.js'),
],
browsers: [chromeLauncher()],
plugins: [moduleMockingPlugin(), nodeResolvePlugin('', false, {})],
});
});

it('can intercept bare modules', async () => {
const rootDir = path.resolve(dirname, 'fixtures', 'bare', 'fixture');
// Define the bare module as duped to force nodeResolve to use the passed rootDir instead of the cwd
Expand Down