Skip to content

Commit 9740778

Browse files
committed
simplify path mock in test
1 parent 8146f0a commit 9740778

1 file changed

Lines changed: 13 additions & 49 deletions

File tree

packages/jest-resolve/src/__tests__/resolve.test.js

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010

1111
jest.mock('../__mocks__/userResolver');
1212

13-
const fs = require('fs');
14-
const path = require('path');
15-
const ModuleMap = require('jest-haste-map').ModuleMap;
16-
const Resolver = require('../');
17-
const userResolver = require('../__mocks__/userResolver');
13+
import fs from 'fs';
14+
import path from 'path';
15+
import {ModuleMap} from 'jest-haste-map';
16+
import userResolver from '../__mocks__/userResolver';
17+
18+
let Resolver = require('../');
1819

1920
beforeEach(() => {
2021
userResolver.mockClear();
@@ -185,56 +186,18 @@ describe('getMockModule', () => {
185186
describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => {
186187
let moduleMap;
187188

188-
const path_methods = {
189-
cache: {},
190-
names: ['dirname', 'resolve', 'parse', 'isAbsolute', 'join'],
191-
platform: '',
192-
};
193-
194-
const save_path = () => {
195-
path_methods.platform =
196-
path.resolve === path.win32.resolve ? 'win32' : 'posix';
197-
198-
path_methods.names.forEach(name => {
199-
path_methods.cache[name] = path[name];
200-
});
201-
};
202-
203-
const restore_path = () => {
204-
const os = path_methods.platform;
205-
206-
path_methods.names.forEach(name => {
207-
path[os][name] = path_methods.cache[name];
208-
path[name] = path_methods.cache[name];
209-
});
210-
};
211-
212-
const update_path = os => {
213-
restore_path();
214-
215-
path_methods.names.forEach(name => {
216-
path[name] = path[os][name];
217-
});
218-
};
219-
220-
beforeAll(() => {
221-
save_path();
222-
});
223-
224189
beforeEach(() => {
190+
jest.resetModules();
225191
moduleMap = new ModuleMap({
226192
duplicates: [],
227193
map: [],
228194
mocks: [],
229195
});
230196
});
231197

232-
afterAll(() => {
233-
restore_path();
234-
});
235-
236198
it('can resolve node modules relative to absolute paths in "moduleDirectories" on Windows platforms', () => {
237-
update_path('win32');
199+
jest.doMock('path', () => path.win32);
200+
Resolver = require('../');
238201

239202
const cwd = 'D:\\project';
240203
const src = 'C:\\path\\to\\node_modules';
@@ -244,14 +207,15 @@ describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => {
244207
const dirs_expected = [
245208
src,
246209
cwd + '\\node_modules',
247-
path.dirname(cwd).replace(/\\$/, '') + '\\node_modules',
210+
path.win32.dirname(cwd).replace(/\\$/, '') + '\\node_modules',
248211
];
249212
const dirs_actual = resolver.getModulePaths(cwd);
250213
expect(dirs_actual).toEqual(expect.arrayContaining(dirs_expected));
251214
});
252215

253216
it('can resolve node modules relative to absolute paths in "moduleDirectories" on Posix platforms', () => {
254-
update_path('posix');
217+
jest.doMock('path', () => path.posix);
218+
Resolver = require('../');
255219

256220
const cwd = '/temp/project';
257221
const src = '/root/path/to/node_modules';
@@ -261,7 +225,7 @@ describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => {
261225
const dirs_expected = [
262226
src,
263227
cwd + '/node_modules',
264-
path.dirname(cwd) + '/node_modules',
228+
path.posix.dirname(cwd) + '/node_modules',
265229
'/node_modules',
266230
];
267231
const dirs_actual = resolver.getModulePaths(cwd);

0 commit comments

Comments
 (0)