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

feat: Add support for Jest specific functions #100

Merged
merged 1 commit into from
Nov 29, 2016
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
38 changes: 37 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default ({ types: t }) => {
const modulePath = mapModule(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
nodePath.replaceWith(t.callExpression(
calleePath.node, [t.stringLiteral(modulePath)]
calleePath.node, [t.stringLiteral(modulePath)],
));
}
}
Expand All @@ -113,6 +113,41 @@ export default ({ types: t }) => {
}
}

function transformJestCalls(nodePath, state, cwd) {
const calleePath = nodePath.get('callee');

const jestMethods = [
'genMockFromModule',
'mock',
'unmock',
];

if (!(
t.isMemberExpression(calleePath.node) &&
t.isIdentifier(calleePath.node.object, { name: 'jest' }) &&
jestMethods.some(methodName => t.isIdentifier(calleePath.node.property, { name: methodName }))
)) {
return;
}

const args = nodePath.get('arguments');
if (!args.length) {
return;
}

const moduleArg = args[0];
if (moduleArg.node.type === 'StringLiteral') {
const modulePath = mapModule(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
const newArgs = [...args].map(a => a.node);
newArgs[0] = t.stringLiteral(modulePath);
nodePath.replaceWith(t.callExpression(
calleePath.node, newArgs,
));
}
}
}

return {
manipulateOptions(babelOptions) {
const findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
Expand Down Expand Up @@ -152,6 +187,7 @@ export default ({ types: t }) => {
}

transformRequireCall(nodePath, state, this.moduleResolverCWD);
transformJestCalls(nodePath, state, this.moduleResolverCWD);

// eslint-disable-next-line no-param-reassign
nodePath.node.seen = true;
Expand Down
34 changes: 17 additions & 17 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,39 @@ describe('module-resolver', () => {
testRequireImport(
'c1',
'./test/examples/components/c1',
transformerOpts
transformerOpts,
);
});

describe('should resolve the sub file path', () => {
testRequireImport(
'sub/sub1',
'./test/examples/components/sub/sub1',
transformerOpts
transformerOpts,
);
});

describe('should resolve the file path while keeping the extension', () => {
testRequireImport(
'sub/sub1.css',
'./test/examples/components/sub/sub1.css',
transformerOpts
transformerOpts,
);
});

describe('should resolve the file path with a filename containing a dot', () => {
testRequireImport(
'sub/custom.modernizr3',
'./test/examples/components/sub/custom.modernizr3',
transformerOpts
transformerOpts,
);
});

describe('should resolve the file path according to a glob', () => {
testRequireImport(
'c1',
'./test/examples/components/c1',
transformerOptsGlob
transformerOptsGlob,
);
});

Expand All @@ -89,15 +89,15 @@ describe('module-resolver', () => {
{
...transformerOpts,
filename: './test/examples/foo/bar/x.js',
}
},
);
});

describe('should not resolve a path outisde of the root directory', () => {
testRequireImport(
'example-file',
'example-file',
transformerOpts
transformerOpts,
);
});
});
Expand All @@ -121,15 +121,15 @@ describe('module-resolver', () => {
testRequireImport(
'utils',
'./src/mylib/subfolder/utils',
transformerOpts
transformerOpts,
);
});

describe('should alias the sub file path', () => {
testRequireImport(
'utils/my-util-file',
'./src/mylib/subfolder/utils/my-util-file',
transformerOpts
transformerOpts,
);
});
});
Expand All @@ -139,15 +139,15 @@ describe('module-resolver', () => {
testRequireImport(
'awesome/components',
'./src/components',
transformerOpts
transformerOpts,
);
});

describe('should alias the sub file path', () => {
testRequireImport(
'awesome/components/my-comp',
'./src/components/my-comp',
transformerOpts
transformerOpts,
);
});
});
Expand All @@ -156,15 +156,15 @@ describe('module-resolver', () => {
testRequireImport(
'utils/custom.modernizr3',
'./src/mylib/subfolder/utils/custom.modernizr3',
transformerOpts
transformerOpts,
);
});

describe('should alias the path with its extension', () => {
testRequireImport(
'awesome/components/my-comp.css',
'./src/components/my-comp.css',
transformerOpts
transformerOpts,
);
});

Expand All @@ -173,15 +173,15 @@ describe('module-resolver', () => {
testRequireImport(
'other-lib',
'other-lib',
transformerOpts
transformerOpts,
);
});

describe('when requiring a specific un-mapped file', () => {
testRequireImport(
'./l/otherLib',
'./l/otherLib',
transformerOpts
transformerOpts,
);
});
});
Expand All @@ -190,15 +190,15 @@ describe('module-resolver', () => {
testRequireImport(
'abstract/thing',
'concrete/thing',
transformerOpts
transformerOpts,
);
});

describe('should support aliasing a node modules', () => {
testRequireImport(
'underscore/map',
'lodash/map',
transformerOpts
transformerOpts,
);
});
});
Expand Down
89 changes: 89 additions & 0 deletions test/jest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-env jest */
import { transform } from 'babel-core'; // eslint-disable-line import/no-extraneous-dependencies
import plugin from '../src';

describe('jest functions', () => {
const transformerOpts = {
babelrc: false,
plugins: [
[plugin, {
root: [
'./test/examples/components',
'./test/examples/foo',
],
alias: {
utils: './src/mylib/subfolder/utils',
},
}],
],
};

describe('jest.mock', () => {
it('should resolve the path based on the root config', () => {
const code = 'jest.mock("c1", () => {});';
const result = transform(code, transformerOpts);

expect(result.code).toBe('jest.mock("./test/examples/components/c1", () => {});');
});

it('should alias the path', () => {
const code = 'jest.mock("utils", () => {});';
const result = transform(code, transformerOpts);

expect(result.code).toBe('jest.mock("./src/mylib/subfolder/utils", () => {});');
});

it('should not change the path', () => {
const code = 'jest.mock("./utils", () => {});';
const result = transform(code, transformerOpts);

expect(result.code).toBe('jest.mock("./utils", () => {});');
});
});

describe('jest.unmock', () => {
it('should resolve the path based on the root config', () => {
const code = 'jest.unmock("c1");';
const result = transform(code, transformerOpts);

expect(result.code).toBe('jest.unmock("./test/examples/components/c1");');
});

it('should alias the path', () => {
const code = 'jest.unmock("utils");';
const result = transform(code, transformerOpts);

expect(result.code).toBe('jest.unmock("./src/mylib/subfolder/utils");');
});

it('should not change the path', () => {
const code = 'jest.unmock("./utils");';
const result = transform(code, transformerOpts);

expect(result.code).toBe('jest.unmock("./utils");');
});
});

describe('jest.genMockFromModule', () => {
it('should resolve the path based on the root config', () => {
const code = 'jest.genMockFromModule("c1");';
const result = transform(code, transformerOpts);

expect(result.code).toBe('jest.genMockFromModule("./test/examples/components/c1");');
});

it('should alias the path', () => {
const code = 'jest.genMockFromModule("utils");';
const result = transform(code, transformerOpts);

expect(result.code).toBe('jest.genMockFromModule("./src/mylib/subfolder/utils");');
});

it('should not change the path', () => {
const code = 'jest.genMockFromModule("./utils");';
const result = transform(code, transformerOpts);

expect(result.code).toBe('jest.genMockFromModule("./utils");');
});
});
});