updated the test
This commit is contained in:
28
tests/setup/babel-plugin-import-meta.cjs
Normal file
28
tests/setup/babel-plugin-import-meta.cjs
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Rewrites `import.meta` so Babel can compile Vite source to CommonJS for Jest.
|
||||
*
|
||||
* `import.meta` is only legal inside a real ES module; babel-jest compiles to
|
||||
* CJS, so every file that reads `import.meta.env` (api/doormile/client.js,
|
||||
* lib/distance.js) is a hard parse error without this.
|
||||
*
|
||||
* It resolves to `globalThis.__VITE_IMPORT_META__` — a plain object the test
|
||||
* setup seeds and individual tests can override, which is the seam that lets a
|
||||
* test exercise `VITE_DOORMILE_URL` / `VITE_OSRM_URL` overrides at all.
|
||||
*/
|
||||
module.exports = function importMetaPlugin({ types: t }) {
|
||||
return {
|
||||
name: 'transform-import-meta-for-jest',
|
||||
visitor: {
|
||||
MetaProperty(path) {
|
||||
if (path.node.meta.name !== 'import' || path.node.property.name !== 'meta') return;
|
||||
path.replaceWith(
|
||||
t.logicalExpression(
|
||||
'||',
|
||||
t.memberExpression(t.identifier('globalThis'), t.identifier('__VITE_IMPORT_META__')),
|
||||
t.objectExpression([t.objectProperty(t.identifier('env'), t.objectExpression([]))])
|
||||
)
|
||||
);
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
1
tests/setup/fileMock.cjs
Normal file
1
tests/setup/fileMock.cjs
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = 'test-file-stub';
|
||||
18
tests/setup/jest.setup.cjs
Normal file
18
tests/setup/jest.setup.cjs
Normal file
@@ -0,0 +1,18 @@
|
||||
require('@testing-library/jest-dom');
|
||||
|
||||
/**
|
||||
* The `import.meta.env` stand-in (see babel-plugin-import-meta.cjs).
|
||||
*
|
||||
* Defaults to production-shaped values — no VITE_ overrides, DEV false — so a
|
||||
* module that branches on an override only does so in the test that asks for
|
||||
* it. Reset before every test so one test's override cannot leak into the next.
|
||||
*/
|
||||
const defaultImportMeta = () => ({ env: { DEV: false, PROD: true, MODE: 'test' } });
|
||||
|
||||
globalThis.__VITE_IMPORT_META__ = defaultImportMeta();
|
||||
|
||||
beforeEach(() => {
|
||||
globalThis.__VITE_IMPORT_META__ = defaultImportMeta();
|
||||
localStorage.clear();
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
2
tests/setup/styleMock.cjs
Normal file
2
tests/setup/styleMock.cjs
Normal file
@@ -0,0 +1,2 @@
|
||||
// CSS/asset imports carry no behaviour worth asserting; Jest cannot parse them.
|
||||
module.exports = {};
|
||||
Reference in New Issue
Block a user