Skip to content

Commit

Permalink
Add more tests (#1)
Browse files Browse the repository at this point in the history
* test: add more tests for no-relative-import

* test: add tests for getPackages()
  • Loading branch information
azz authored Dec 14, 2017
1 parent 8669e77 commit e50c927
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 18 deletions.
28 changes: 14 additions & 14 deletions src/rules/no-relative-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ export const create = context => {
}

const pkg = packages.find(pkg => isInside(resolvedPath, pkg.fsPath));
if (pkg) {
const subPackagePath = path.relative(pkg.fsPath, resolvedPath);
context.report({
node,
message: `Import for monorepo package '${
pkg.name
}' should be absolute.`,
fix: fixer => {
fixer.replaceText(
node,
`${pkg.name}${subPackagePath !== '.' ? '/' + subPackagePath : ''}`
);
},
});
if (!pkg) {
return;
}

const subPackagePath = path.relative(pkg.fsPath, resolvedPath);
context.report({
node,
message: `Import for monorepo package '${pkg.name}' should be absolute.`,
fix: fixer => {
fixer.replaceText(
node,
`${pkg.name}${subPackagePath !== '.' ? '/' + subPackagePath : ''}`
);
},
});
}, moduleUtilOptions);
};

Expand Down
27 changes: 27 additions & 0 deletions test/__snapshots__/get-packages.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getPackages() lerna matches snapshot 1`] = `
Array [
Object {
"fsPath": "<cwd>/test/fixture/lerna/packages/bar",
"name": "bar",
},
Object {
"fsPath": "<cwd>/test/fixture/lerna/packages/foo",
"name": "foo",
},
]
`;

exports[`getPackages() yarn matches snapshot 1`] = `
Array [
Object {
"fsPath": "<cwd>/test/fixture/yarn/packages/bar",
"name": "bar",
},
Object {
"fsPath": "<cwd>/test/fixture/yarn/packages/foo",
"name": "foo",
},
]
`;
3 changes: 3 additions & 0 deletions test/fixture/lerna/lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"packages": ["packages/*"]
}
Empty file.
3 changes: 3 additions & 0 deletions test/fixture/lerna/packages/bar/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "bar"
}
Empty file.
3 changes: 3 additions & 0 deletions test/fixture/lerna/packages/foo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "foo"
}
23 changes: 23 additions & 0 deletions test/get-packages.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import getPackages from '../src/get-packages';
import path from 'path';

const LERNA_DIR = path.join(__dirname, 'fixture/lerna');
const YARN_DIR = path.join(__dirname, 'fixture/yarn');

expect.addSnapshotSerializer({
test: value =>
typeof value === 'string' &&
(value.indexOf('\\') > -1 || value.indexOf(process.cwd()) > -1),
print: (value, serializer) =>
serializer(value.replace(process.cwd(), '<cwd>').replace(/\\/g, '/')),
});

describe('getPackages()', () => {
test('lerna matches snapshot', () => {
expect(getPackages(LERNA_DIR)).toMatchSnapshot();
});

test('yarn matches snapshot', () => {
expect(getPackages(YARN_DIR)).toMatchSnapshot();
});
});
3 changes: 2 additions & 1 deletion test/rules/no-internal-import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import * as monorepo from '../../src';

const RULE = 'no-internal-import';
const ERROR = { message: `Import for monorepo package 'foo' is internal.` };
const fixtures = path.join(__dirname, '../fixture/yarn');

process.cwd = jest.fn(() => fixtures);
Expand All @@ -23,7 +24,7 @@ ruleTester.run(RULE, monorepo.rules[RULE], {
{
code: `import pkg from 'foo/src/pkg'`,
filename: path.join(fixtures, 'packages/bar/index.js'),
errors: [{ message: `Import for monorepo package 'foo' is internal.` }],
errors: [ERROR],
},
],
});
29 changes: 26 additions & 3 deletions test/rules/no-relative-import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import path from 'path';
import * as monorepo from '../../src';

const RULE = 'no-relative-import';
const ERROR = {
message: `Import for monorepo package 'foo' should be absolute.`,
};
const fixtures = path.join(__dirname, '../fixture/yarn');

process.cwd = jest.fn(() => fixtures);
Expand All @@ -17,15 +20,35 @@ ruleTester.run(RULE, monorepo.rules[RULE], {
code: `import foo from 'foo'`,
filename: path.join(fixtures, 'packages/bar/index.js'),
},
{
code: `const foo = require('foo')`,
filename: path.join(fixtures, 'packages/bar/index.js'),
options: [{ commonjs: true }],
},
{
code: `define(['foo'], factory)`,
filename: path.join(fixtures, 'packages/bar/index.js'),
options: [{ amd: true }],
},
],

invalid: [
{
code: `import foo from '../foo'`,
filename: path.join(fixtures, 'packages/bar/index.js'),
errors: [
{ message: `Import for monorepo package 'foo' should be absolute.` },
],
errors: [ERROR],
},
{
code: `const foo = require('../foo')`,
filename: path.join(fixtures, 'packages/bar/index.js'),
options: [{ commonjs: true }],
errors: [ERROR],
},
{
code: `define(['../foo'], factory)`,
filename: path.join(fixtures, 'packages/bar/index.js'),
options: [{ amd: true }],
errors: [ERROR],
},
],
});

0 comments on commit e50c927

Please sign in to comment.