diff --git a/packages/test-runner-module-mocking/test/fixtures/bug-cache/browser-test-1.js b/packages/test-runner-module-mocking/test/fixtures/bug-cache/browser-test-1.js new file mode 100644 index 000000000..fdc4bae47 --- /dev/null +++ b/packages/test-runner-module-mocking/test/fixtures/bug-cache/browser-test-1.js @@ -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); + }); +}); diff --git a/packages/test-runner-module-mocking/test/fixtures/bug-cache/browser-test-2.js b/packages/test-runner-module-mocking/test/fixtures/bug-cache/browser-test-2.js new file mode 100644 index 000000000..06f2a5bf9 --- /dev/null +++ b/packages/test-runner-module-mocking/test/fixtures/bug-cache/browser-test-2.js @@ -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'); + }); +}); diff --git a/packages/test-runner-module-mocking/test/fixtures/bug-cache/fixture/getTimeOfDay.js b/packages/test-runner-module-mocking/test/fixtures/bug-cache/fixture/getTimeOfDay.js new file mode 100644 index 000000000..cd3bbe510 --- /dev/null +++ b/packages/test-runner-module-mocking/test/fixtures/bug-cache/fixture/getTimeOfDay.js @@ -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'; +} diff --git a/packages/test-runner-module-mocking/test/fixtures/bug-cache/fixture/time-library.js b/packages/test-runner-module-mocking/test/fixtures/bug-cache/fixture/time-library.js new file mode 100644 index 000000000..636846664 --- /dev/null +++ b/packages/test-runner-module-mocking/test/fixtures/bug-cache/fixture/time-library.js @@ -0,0 +1,3 @@ +export function getCurrentHour() { + return 2; +} diff --git a/packages/test-runner-module-mocking/test/moduleMockingPlugin.test.ts b/packages/test-runner-module-mocking/test/moduleMockingPlugin.test.ts index 2b365dcc7..385d43a9b 100644 --- a/packages/test-runner-module-mocking/test/moduleMockingPlugin.test.ts +++ b/packages/test-runner-module-mocking/test/moduleMockingPlugin.test.ts @@ -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