mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-18 05:18:39 +08:00
feat: initial angular support + clean up tests
This commit is contained in:

committed by
Nathan Walker

parent
e8888719be
commit
523f6bbef2
@ -1,5 +1,15 @@
|
||||
// define our global helpers
|
||||
declare global {
|
||||
function mockFile(path: string, content: string);
|
||||
}
|
||||
|
||||
// enable TEST mode
|
||||
global.__TEST__ = true;
|
||||
|
||||
// we are mocking the cwd for the tests, since webpack needs absolute paths
|
||||
// and we don't want them in tests
|
||||
import dedent from 'ts-dedent';
|
||||
|
||||
process.cwd = () => '__jest__';
|
||||
|
||||
// a virtual mock for package.json
|
||||
@ -22,10 +32,51 @@ jest.mock('cosmiconfig', () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('path', () => ({
|
||||
...jest.requireActual('path'),
|
||||
// we are mocking resolve to just simply join the paths for tests
|
||||
resolve(...args) {
|
||||
return args.join('/');
|
||||
},
|
||||
}));
|
||||
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;
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const mockedFiles: { [path: string]: string } = {};
|
||||
|
||||
global.mockFile = 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);
|
||||
};
|
||||
|
||||
jest.mock('fs', () => {
|
||||
const fs = jest.requireActual('fs');
|
||||
const unionFS = require('unionfs').default;
|
||||
unionFS.reset = () => {
|
||||
unionFS.fss = [fs];
|
||||
};
|
||||
|
||||
return unionFS.use(fs);
|
||||
});
|
||||
|
Reference in New Issue
Block a user