Skip to content

Commit

Permalink
test(auto-install): fix yarn tests (#147)
Browse files Browse the repository at this point in the history
* test(auto-install): fix yarn tests

* chore: reinstate line break normalization

* chore: try to normalize echo between OSes
  • Loading branch information
shellscape committed Jan 9, 2020
1 parent fe0f67a commit 20189f2
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 64 deletions.
11 changes: 6 additions & 5 deletions packages/auto-install/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ function exec(cmd) {

module.exports = function autoInstall(opts = {}) {
const defaults = {
// intentionally undocumented options. used for tests
commands: {
npm: 'npm install',
yarn: 'yarn add'
},
manager: fs.existsSync('yarn.lock') ? 'yarn' : 'npm',
pkgFile: path.resolve(opts.pkgFile || 'package.json')
};

const options = Object.assign({}, defaults, opts);
const { manager, pkgFile } = options;
const validManagers = ['npm', 'yarn'];
const commands = {
npm: 'npm install',
yarn: 'yarn add'
};
let pkg;

if (!validManagers.includes(manager)) {
Expand All @@ -45,7 +46,7 @@ module.exports = function autoInstall(opts = {}) {
}

const installed = new Set(Object.keys(pkg.dependencies || {}).concat(builtinModules));
const cmd = commands[manager];
const cmd = options.commands[manager];

return {
name: 'auto-install',
Expand Down
1 change: 0 additions & 1 deletion packages/auto-install/test/fixtures/npm-bare/package.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/auto-install/test/fixtures/yarn-bare/package.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/auto-install/test/fixtures/yarn/package.json

This file was deleted.

28 changes: 0 additions & 28 deletions packages/auto-install/test/snapshots/yarn-bare.js.md

This file was deleted.

Binary file removed packages/auto-install/test/snapshots/yarn-bare.js.snap
Binary file not shown.
19 changes: 0 additions & 19 deletions packages/auto-install/test/snapshots/yarn.js.md

This file was deleted.

Binary file removed packages/auto-install/test/snapshots/yarn.js.snap
Binary file not shown.
12 changes: 8 additions & 4 deletions packages/auto-install/test/yarn-bare.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ test('yarn, bare', async (t) => {
file,
format: 'cjs'
},
plugins: [autoInstall({ manager: 'yarn' }), resolve()]
plugins: [
// mock the call to yarn here. yarn has had consistent issues in this test env
autoInstall({ manager: 'yarn', commands: { yarn: 'echo yarn.bare > yarn.lock' } }),
resolve()
]
});
const lock = readFileSync('yarn.lock', 'utf-8').replace(/\r\n/g, '\n');
t.snapshot(readFileSync('package.json', 'utf-8').replace(/\r\n/g, '\n'));
t.snapshot(lock);
const lockFile = readFileSync('yarn.lock', 'utf-8');
// snapshots for this are a nightmare cross-platform
t.truthy(/yarn\.bare\s+node-noop/.test(lockFile));
});

test.after(async () => {
Expand Down
11 changes: 6 additions & 5 deletions packages/auto-install/test/yarn.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const autoInstall = require('..');
const cwd = join(__dirname, 'fixtures/yarn');
const file = join(cwd, 'output/bundle.js');
const input = join(cwd, '../input.js');
const lockFile = join(cwd, 'yarn.lock');

process.chdir(cwd);

Expand All @@ -22,13 +21,15 @@ test('yarn', async (t) => {
file,
format: 'cjs'
},
plugins: [autoInstall(), resolve()]
// mock the call to yarn here. yarn has had consistent issues in this test env
plugins: [autoInstall({ commands: { yarn: 'echo yarn > yarn.lock' } }), resolve()]
});
const lock = readFileSync('yarn.lock', 'utf-8').replace(/\r\n/g, '\n');
t.snapshot(lock);
const lockFile = readFileSync('yarn.lock', 'utf-8');
// snapshots for this are a nightmare cross-platform
t.truthy(/yarn\s+node-noop/.test(lockFile));
});

test.after(async () => {
await del(['node_modules', 'package.json']);
writeFileSync(lockFile, '');
writeFileSync('yarn.lock', '');
});

0 comments on commit 20189f2

Please sign in to comment.