Use escape hatch for mocked modules
Reviewed By: davidaurelio Differential Revision: D4869222 fbshipit-source-id: d2cf66296a26710630f0d0f6c8efc7ff3ed97bc5
This commit is contained in:
parent
3c85a6b902
commit
abcd1b6370
|
@ -12,8 +12,8 @@
|
|||
jest.disableAutomock();
|
||||
jest.useRealTimers();
|
||||
|
||||
const fetchMock = jest.fn();
|
||||
jest.mock('node-fetch', () => fetchMock);
|
||||
const mockFetch = jest.fn();
|
||||
jest.mock('node-fetch', () => mockFetch);
|
||||
|
||||
const {URIBasedGlobalTransformCache} = require('../GlobalTransformCache');
|
||||
const FetchError = require('node-fetch/lib/fetch-error');
|
||||
|
@ -82,19 +82,19 @@ describe('GlobalTransformCache', () => {
|
|||
});
|
||||
|
||||
beforeEach(() => {
|
||||
fetchMock.mockReset();
|
||||
mockFetch.mockReset();
|
||||
});
|
||||
|
||||
it('fetches result', async () => {
|
||||
fetchMock.mockImplementation(defaultFetchMockImpl);
|
||||
mockFetch.mockImplementation(defaultFetchMockImpl);
|
||||
const result = await URIBasedGlobalTransformCache
|
||||
.fetchResultFromURI('http://globalcache.com/foo');
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('retries once on timeout', async () => {
|
||||
fetchMock.mockImplementation(async uri => {
|
||||
fetchMock.mockImplementation(defaultFetchMockImpl);
|
||||
mockFetch.mockImplementation(async uri => {
|
||||
mockFetch.mockImplementation(defaultFetchMockImpl);
|
||||
throw new FetchError('timeout!', 'request-timeout');
|
||||
});
|
||||
const result = await URIBasedGlobalTransformCache
|
||||
|
|
|
@ -21,14 +21,14 @@ const imurmurhash = require('imurmurhash');
|
|||
const crypto = require('crypto');
|
||||
const jsonStableStringify = require('json-stable-stringify');
|
||||
|
||||
const memoryFS = new Map();
|
||||
const mockFS = new Map();
|
||||
|
||||
jest.mock('fs', () => ({
|
||||
readFileSync(filePath) {
|
||||
return memoryFS.get(filePath);
|
||||
return mockFS.get(filePath);
|
||||
},
|
||||
unlinkSync(filePath) {
|
||||
memoryFS.delete(filePath);
|
||||
mockFS.delete(filePath);
|
||||
},
|
||||
readdirSync(dirPath) {
|
||||
// Not required for it to work.
|
||||
|
@ -38,7 +38,7 @@ jest.mock('fs', () => ({
|
|||
|
||||
jest.mock('write-file-atomic', () => ({
|
||||
sync(filePath, data) {
|
||||
memoryFS.set(filePath, data.toString());
|
||||
mockFS.set(filePath, data.toString());
|
||||
},
|
||||
}));
|
||||
|
||||
|
@ -56,7 +56,7 @@ describe('TransformCache', () => {
|
|||
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
memoryFS.clear();
|
||||
mockFS.clear();
|
||||
TransformCache = require('../TransformCache');
|
||||
});
|
||||
|
||||
|
|
|
@ -19,9 +19,10 @@ jest
|
|||
;
|
||||
|
||||
// This doesn't have state, and it's huge (Babel) so it's much faster to
|
||||
// require it only once.
|
||||
const extractDependencies = require('../../JSTransformer/worker/extract-dependencies');
|
||||
jest.mock('../../JSTransformer/worker/extract-dependencies', () => extractDependencies);
|
||||
// require it only once. The variable name is prefixed with "mock" as an escape-hatch
|
||||
// for babel-plugin-jest-hoist.
|
||||
const mockExtractDependencies = require('../../JSTransformer/worker/extract-dependencies');
|
||||
jest.mock('../../JSTransformer/worker/extract-dependencies', () => mockExtractDependencies);
|
||||
|
||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
|
||||
|
||||
|
@ -119,7 +120,7 @@ describe('DependencyGraph', function() {
|
|||
return new Promise(resolve => {
|
||||
let deps = {dependencies: [], dependencyOffsets: []};
|
||||
if (!module.path.endsWith('.json')) {
|
||||
deps = extractDependencies(sourceCode);
|
||||
deps = mockExtractDependencies(sourceCode);
|
||||
}
|
||||
resolve({...deps, code: sourceCode});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue