Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect duplicate mock warning. #8167

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions e2e/__tests__/hasteMapMockChanged.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import path from 'path';
import JestHasteMap from 'jest-haste-map';
import {cleanup, writeFiles} from '../Utils';

// Directory must be here for Watchman to be enabled.
const DIR = path.resolve(__dirname, 'haste_map_mock_changed');

beforeEach(() => cleanup(DIR));
afterEach(() => cleanup(DIR));

test('should not warn when a mock file changes', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like jest-haste-map tests as e2e tests. The tests in packages/jest-haste-map are really hard on testing internals - this way it's easier to test it behaves properly in a way observable from the outside

const hasteConfig = {
computeSha1: false,
extensions: ['js', 'json', 'png'],
forceNodeFilesystemAPI: false,
ignorePattern: / ^/,
maxWorkers: 2,
mocksPattern: '__mocks__',
name: 'tmp_' + Date.now(),
platforms: [],
retainAllFiles: false,
rootDir: DIR,
roots: [DIR],
throwOnModuleCollision: true,
useWatchman: true,
watch: false,
};

// Populate the cache.
writeFiles(DIR, {
'__mocks__/fs.js': '"foo fs"',
});
await new JestHasteMap(hasteConfig).build();

// This will throw if the mock file being updated triggers a warning.
writeFiles(DIR, {
'__mocks__/fs.js': '"foo fs!"',
});
await new JestHasteMap(hasteConfig).build();
});
30 changes: 17 additions & 13 deletions packages/jest-haste-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,20 +574,24 @@ class HasteMap extends EventEmitter {

if (existingMockPath) {
const secondMockPath = fastPath.relative(rootDir, filePath);
const method = this._options.throwOnModuleCollision ? 'error' : 'warn';

this._console[method](
[
'jest-haste-map: duplicate manual mock found: ' + mockPath,
' The following files share their name; please delete one of them:',
' * <rootDir>' + path.sep + existingMockPath,
' * <rootDir>' + path.sep + secondMockPath,
'',
].join('\n'),
);
if (existingMockPath !== secondMockPath) {
const method = this._options.throwOnModuleCollision
? 'error'
: 'warn';

this._console[method](
[
'jest-haste-map: duplicate manual mock found: ' + mockPath,
' The following files share their name; please delete one of them:',
' * <rootDir>' + path.sep + existingMockPath,
' * <rootDir>' + path.sep + secondMockPath,
'',
].join('\n'),
);

if (this._options.throwOnModuleCollision) {
throw new DuplicateError(existingMockPath, secondMockPath);
if (this._options.throwOnModuleCollision) {
throw new DuplicateError(existingMockPath, secondMockPath);
}
}
}

Expand Down