Skip to content

Commit

Permalink
fix(roc-plugin-repo): Fix Jest resolver when using moduleNameMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
dlmr committed Jan 22, 2018
1 parent eced6ff commit 3f4fb07
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions extensions/roc-plugin-repo/src/commands/utils/jest/roc-resolver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This version of Roc might not be the same version as is used in the project to launch the CLI
import fs from 'fs';
import resolveFrom from 'resolve-from';

// This version of Roc might not be the same version as is used in the project to launch the CLI
require('roc').runCli({
invoke: false,
argv: JSON.parse(process.env.ROC_INITAL_ARGV),
Expand All @@ -14,6 +15,24 @@ const jestDefaultResolver = require(resolveFrom(
)).default;
const callsites = require('callsites');

// This finds the line number for an error that Jest throws when there is a problem
// with moduleNameMapper resolving. We use this to find a reference for when we should
// short-circuit the resolver since it is called in two places for _resolveStubModuleName
const lineNumberForModuleMapperError =
fs
.readFileSync(
resolveFrom(require.resolve('jest'), 'jest-resolve/build/index.js'),
'utf8',
)
.split('\n')
.findIndex(row => /Could not locate module/.test(row)) + 1;

if (lineNumberForModuleMapperError === 0) {
throw new Error(
'The integration between Jest and roc-plugin-repo is out of date, please update roc-plugin-repo.',
);
}

module.exports = function customJestResolver(path, options) {
// This logic manages a bug in Jest that makes mocking fail and should be removed
// as soon as this problem is addressed. What we are doing here is that we disable
Expand All @@ -27,7 +46,11 @@ module.exports = function customJestResolver(path, options) {
// don't want but adding detection on line number is too fragile.
//
// https://github.com/facebook/jest/issues/4985
if (callsites()[2].getMethodName() === '_resolveStubModuleName') {
const cs = callsites()[2];
if (
cs.getMethodName() === '_resolveStubModuleName' &&
cs.getLineNumber() > lineNumberForModuleMapperError
) {
return null;
}
// return jestDefaultResolver(path, options);
Expand Down

0 comments on commit 3f4fb07

Please sign in to comment.