Skip to content

Commit

Permalink
Add tests for command "run" (yarnpkg#821)
Browse files Browse the repository at this point in the history
* Add basic tests for command "run"

* Add run test with args and space in path

* Expect executeLifecycleScript to be called properly
  • Loading branch information
AlicanC authored and bestander committed Oct 28, 2016
1 parent 420a155 commit 782fd84
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 0 deletions.
87 changes: 87 additions & 0 deletions __tests__/commands/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* @flow */

import {run} from '../../src/cli/commands/run.js';
import * as reporters from '../../src/reporters/index.js';
import Config from '../../src/config.js';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;

const path = require('path');

const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'run');

jest.mock('../../src/util/execute-lifecycle-script');
const executeLifecycleScript = (require('../../src/util/execute-lifecycle-script').default: $FlowFixMe);

async function runRun(
flags: Object,
args: Array<string>,
name: string,
): Promise<Config> {
const cwd = path.join(fixturesLoc, name);
const reporter = new reporters.NoopReporter();
const config = new Config(reporter);
await config.init({cwd});
await run(config, reporter, flags, args);
return config;
}

beforeEach(() => {
executeLifecycleScript.mockClear();
});

it('run should run script', async (): Promise<void> => {
const config = await runRun({}, ['some-script'], 'run-should-run-script');

const expectedCall = [
'some-script',
config,
config.cwd,
`echo success `,
];

expect(executeLifecycleScript.mock.calls.length).toEqual(1);
expect(executeLifecycleScript).toBeCalledWith(...expectedCall);
});

it('run should run binary', async (): Promise<void> => {
const config = await runRun({}, ['some-binary'], 'run-should-run-binary');

const expectedCall = [
'some-binary',
config,
config.cwd,
`"${path.join(config.cwd, 'node_modules/.bin/some-binary')}" `,
];

expect(executeLifecycleScript.mock.calls.length).toEqual(1);
expect(executeLifecycleScript).toBeCalledWith(...expectedCall);
});

it('run should run binary with args', async (): Promise<void> => {
const config = await runRun({}, ['some-binary', '--test-arg'], 'run-should-run-binary-with-args');

const expectedCall = [
'some-binary',
config,
config.cwd,
`"${path.join(config.cwd, 'node_modules/.bin/some-binary')}" --test-arg`,
];

expect(executeLifecycleScript.mock.calls.length).toEqual(1);
expect(executeLifecycleScript).toBeCalledWith(...expectedCall);
});

it('run should run binary with space in path', async (): Promise<void> => {
const config = await runRun({}, ['some-binary'], 'run should run binary with space in path');

const expectedCall = [
'some-binary',
config,
config.cwd,
`"${path.join(config.cwd, 'node_modules/.bin/some-binary')}" `,
];

expect(executeLifecycleScript.mock.calls.length).toEqual(1);
expect(executeLifecycleScript).toBeCalledWith(...expectedCall);
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions __tests__/fixtures/run/run-should-run-binary/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
5 changes: 5 additions & 0 deletions __tests__/fixtures/run/run-should-run-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"some-script": "echo success"
}
}

0 comments on commit 782fd84

Please sign in to comment.