mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-14 10:01:08 +08:00
chore: organize test files
This commit is contained in:
12
packages/webpack5/scripts/jest.globals.d.ts
vendored
Normal file
12
packages/webpack5/scripts/jest.globals.d.ts
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
// define test-specific globals here
|
||||
|
||||
declare namespace jest {
|
||||
interface Matchers<R, T> {
|
||||
toHaveBeenWarned(): R;
|
||||
toHaveBeenPrinted(): R;
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
function mockFile(path: string, content: string);
|
||||
}
|
42
packages/webpack5/scripts/jest.mockFiles.ts
Normal file
42
packages/webpack5/scripts/jest.mockFiles.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import dedent from 'ts-dedent';
|
||||
|
||||
const mockedFiles: { [path: string]: string } = {};
|
||||
|
||||
export function mockFile(path, content) {
|
||||
const unionFS = require('unionfs').default;
|
||||
const Volume = require('memfs').Volume;
|
||||
|
||||
// reset to fs
|
||||
unionFS.reset();
|
||||
|
||||
// add mocked file
|
||||
mockedFiles[path] = dedent(content);
|
||||
|
||||
// create new volume
|
||||
const vol = Volume.fromJSON(mockedFiles, '__jest__');
|
||||
|
||||
// use the new volume
|
||||
unionFS.use(vol as any);
|
||||
}
|
||||
|
||||
// a virtual mock for package.json
|
||||
jest.mock(
|
||||
'__jest__/package.json',
|
||||
() => ({
|
||||
main: 'src/app.js',
|
||||
devDependencies: {
|
||||
typescript: '*',
|
||||
},
|
||||
}),
|
||||
{ virtual: true }
|
||||
);
|
||||
|
||||
jest.mock('fs', () => {
|
||||
const fs = jest.requireActual('fs');
|
||||
const unionFS = require('unionfs').default;
|
||||
unionFS.reset = () => {
|
||||
unionFS.fss = [fs];
|
||||
};
|
||||
|
||||
return unionFS.use(fs);
|
||||
});
|
64
packages/webpack5/scripts/jest.mockWarn.ts
Normal file
64
packages/webpack5/scripts/jest.mockWarn.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import { printExpected, printReceived } from 'jest-matcher-utils';
|
||||
import dedent from 'ts-dedent';
|
||||
|
||||
expect.extend({
|
||||
toHaveBeenWarned(received: string) {
|
||||
asserted.add(received);
|
||||
const passed = warnSpy.mock.calls
|
||||
.map((args) => args[1])
|
||||
.some((arg) => arg.indexOf(received) > -1);
|
||||
if (passed) {
|
||||
return {
|
||||
pass: true,
|
||||
message() {
|
||||
return `expected ${printReceived(received)} not to have been warned`;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const warnings = warnSpy.mock.calls.map((args) => args[1]).join('\n\n');
|
||||
return {
|
||||
pass: false,
|
||||
message() {
|
||||
return dedent`
|
||||
expected ${printExpected(received)} to have been warned.
|
||||
|
||||
Actual warnings:
|
||||
|
||||
${warnings}
|
||||
`;
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
let warnSpy: any;
|
||||
let asserted = new Set([]);
|
||||
beforeEach(() => {
|
||||
asserted.clear();
|
||||
|
||||
warnSpy = jest.spyOn(console, 'warn');
|
||||
|
||||
warnSpy.mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
const assertedArray = Array.from(asserted);
|
||||
const nonAssertedWarns = warnSpy.mock.calls
|
||||
.map((args) => args[1])
|
||||
.filter((received) => {
|
||||
return !assertedArray.some((assertedMessage) => {
|
||||
return received.indexOf(assertedMessage) > -1;
|
||||
});
|
||||
});
|
||||
|
||||
warnSpy.mockRestore();
|
||||
|
||||
if (nonAssertedWarns.length) {
|
||||
throw new Error(dedent`
|
||||
Test case printed unexpected warnings:
|
||||
|
||||
${printReceived(nonAssertedWarns.join('\n\n'))}
|
||||
`);
|
||||
}
|
||||
});
|
35
packages/webpack5/scripts/jest.setup.ts
Normal file
35
packages/webpack5/scripts/jest.setup.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import './jest.mockFiles';
|
||||
// we are mocking the cwd for the tests, since webpack needs absolute paths
|
||||
// and we don't want them in tests
|
||||
process.cwd = () => '__jest__';
|
||||
|
||||
jest.mock('cosmiconfig', () => ({
|
||||
cosmiconfigSync(moduleName) {
|
||||
return {
|
||||
search() {
|
||||
// no-op in tests
|
||||
return null;
|
||||
},
|
||||
};
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('path', () => {
|
||||
const path = jest.requireActual('path');
|
||||
return {
|
||||
...path,
|
||||
resolve(...args) {
|
||||
if (args[0] === '__jest__') {
|
||||
return path.join(...args);
|
||||
}
|
||||
|
||||
const resolved = path.resolve(...args);
|
||||
if (resolved.includes('__jest__')) {
|
||||
const li = resolved.lastIndexOf('__jest__');
|
||||
return resolved.substr(li);
|
||||
}
|
||||
|
||||
return resolved;
|
||||
},
|
||||
};
|
||||
});
|
Reference in New Issue
Block a user