mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-19 14:20:22 +08:00
Merge branch 'feat/webpack5' into pr/9133
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import { copyRules } from '../src/helpers/copyRules';
|
||||
import { copyRules, additionalCopyRules } from '../src/helpers/copyRules';
|
||||
|
||||
afterEach(() => {
|
||||
// Clear copy rules
|
||||
copyRules.clear();
|
||||
additionalCopyRules.length = 0;
|
||||
});
|
||||
|
4
packages/webpack5/scripts/jest.globals.d.ts
vendored
4
packages/webpack5/scripts/jest.globals.d.ts
vendored
@ -6,7 +6,3 @@ declare namespace jest {
|
||||
toHaveBeenPrinted(): R;
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
function mockFile(path: string, content: string);
|
||||
}
|
||||
|
@ -1,42 +0,0 @@
|
||||
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);
|
||||
});
|
@ -1,4 +1,3 @@
|
||||
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__';
|
||||
@ -14,13 +13,51 @@ jest.mock('cosmiconfig', () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('../src/helpers/config.ts', () => ({
|
||||
getValue(key, defaultValue) {
|
||||
return defaultValue;
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('os', () => {
|
||||
const os = jest.requireActual('os');
|
||||
|
||||
return {
|
||||
...os,
|
||||
networkInterfaces() {
|
||||
return {
|
||||
in0: [
|
||||
{
|
||||
address: '127.0.0.1',
|
||||
family: 'IPv4',
|
||||
},
|
||||
{
|
||||
address: 'in0-ipv6-should-not-use',
|
||||
family: 'IPv6',
|
||||
},
|
||||
],
|
||||
in1: [
|
||||
{
|
||||
address: '192.168.0.10',
|
||||
family: 'IPv4',
|
||||
},
|
||||
{
|
||||
address: 'in1-ipv6-should-not-use',
|
||||
family: 'IPv6',
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
jest.mock('path', () => {
|
||||
const path = jest.requireActual('path');
|
||||
return {
|
||||
...path,
|
||||
resolve(...args) {
|
||||
if (args[0] === '__jest__') {
|
||||
return path.join(...args);
|
||||
return path.join(...args.filter(Boolean));
|
||||
}
|
||||
|
||||
const resolved = path.resolve(...args);
|
||||
@ -29,7 +66,35 @@ jest.mock('path', () => {
|
||||
return resolved.substr(li);
|
||||
}
|
||||
|
||||
// handle resolutions with __dirname
|
||||
// used in base config's resolveLoader
|
||||
const root = path.resolve(__dirname, '..');
|
||||
if (resolved.startsWith(root)) {
|
||||
const newPath = resolved.replace(root, '__jest__');
|
||||
|
||||
if (newPath.startsWith('__jest__/src')) {
|
||||
return newPath.replace(
|
||||
'__jest__/src',
|
||||
'__jest__/node_modules/@nativescript/webpack/dist'
|
||||
);
|
||||
}
|
||||
|
||||
return newPath;
|
||||
}
|
||||
|
||||
return resolved;
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
// a virtual mock for package.json
|
||||
jest.mock(
|
||||
'__jest__/package.json',
|
||||
() => ({
|
||||
main: 'src/app.js',
|
||||
devDependencies: {
|
||||
typescript: '*',
|
||||
},
|
||||
}),
|
||||
{ virtual: true }
|
||||
);
|
||||
|
Reference in New Issue
Block a user