From 46cd7b53bc3db82d400ae1d9ef9f24a26b4ef5f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alican=20=C3=87ubuk=C3=A7uo=C4=9Flu?= Date: Sat, 22 Oct 2016 21:24:57 +0300 Subject: [PATCH] Expect executeLifecycleScript to be called properly --- __tests__/commands/run.js | 68 ++++++++++++++++--- .../node_modules/.bin/echo | 3 - .../node_modules/.bin/some-binary | 3 + .../{echo => some-binary}/lib/index.js | 0 .../{echo => some-binary}/package.json | 0 .../node_modules/.bin/echo | 3 - .../node_modules/.bin/some-binary | 3 + .../{echo => some-binary}/lib/index.js | 0 .../{echo => some-binary}/package.json | 0 .../node_modules/.bin/echo | 3 - .../node_modules/.bin/some-binary | 3 + .../{echo => some-binary}/lib/index.js | 0 .../{echo => some-binary}/package.json | 0 13 files changed, 67 insertions(+), 19 deletions(-) delete mode 100755 __tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/echo create mode 100755 __tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/some-binary rename __tests__/fixtures/run/run should run binary with space in path/node_modules/{echo => some-binary}/lib/index.js (100%) rename __tests__/fixtures/run/run should run binary with space in path/node_modules/{echo => some-binary}/package.json (100%) delete mode 100755 __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/echo create mode 100755 __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/some-binary rename __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/{echo => some-binary}/lib/index.js (100%) rename __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/{echo => some-binary}/package.json (100%) delete mode 100755 __tests__/fixtures/run/run-should-run-binary/node_modules/.bin/echo create mode 100755 __tests__/fixtures/run/run-should-run-binary/node_modules/.bin/some-binary rename __tests__/fixtures/run/run-should-run-binary/node_modules/{echo => some-binary}/lib/index.js (100%) rename __tests__/fixtures/run/run-should-run-binary/node_modules/{echo => some-binary}/package.json (100%) diff --git a/__tests__/commands/run.js b/__tests__/commands/run.js index 436fa8a5fa..b3dadbdc07 100644 --- a/__tests__/commands/run.js +++ b/__tests__/commands/run.js @@ -10,30 +10,78 @@ 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, name: string, -): Promise { +): Promise { const cwd = path.join(fixturesLoc, name); const reporter = new reporters.NoopReporter(); const config = new Config(reporter); await config.init({cwd}); - return run(config, reporter, flags, args); + await run(config, reporter, flags, args); + return config; } -test.concurrent('run should run script', (): Promise => { - return runRun({}, ['some-script'], 'run-should-run-script'); +beforeEach(() => { + executeLifecycleScript.mockClear(); }); -test.concurrent('run should run binary', (): Promise => { - return runRun({}, ['echo'], 'run-should-run-binary'); +it('run should run script', async (): Promise => { + 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); }); -test.concurrent('run should run binary with args', (): Promise => { - return runRun({}, ['echo', '--test-arg'], 'run-should-run-binary-with-args'); +it('run should run binary', async (): Promise => { + 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); }); -test.concurrent('run should run binary with space in path', (): Promise => { - return runRun({}, ['echo'], 'run should run binary with space in path'); +it('run should run binary with args', async (): Promise => { + 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 => { + 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); }); diff --git a/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/echo b/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/echo deleted file mode 100755 index bc228dca41..0000000000 --- a/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/echo +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env node - -require("../echo"); diff --git a/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/some-binary b/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/some-binary new file mode 100755 index 0000000000..83a3c57143 --- /dev/null +++ b/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/some-binary @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require("../some-binary"); diff --git a/__tests__/fixtures/run/run should run binary with space in path/node_modules/echo/lib/index.js b/__tests__/fixtures/run/run should run binary with space in path/node_modules/some-binary/lib/index.js similarity index 100% rename from __tests__/fixtures/run/run should run binary with space in path/node_modules/echo/lib/index.js rename to __tests__/fixtures/run/run should run binary with space in path/node_modules/some-binary/lib/index.js diff --git a/__tests__/fixtures/run/run should run binary with space in path/node_modules/echo/package.json b/__tests__/fixtures/run/run should run binary with space in path/node_modules/some-binary/package.json similarity index 100% rename from __tests__/fixtures/run/run should run binary with space in path/node_modules/echo/package.json rename to __tests__/fixtures/run/run should run binary with space in path/node_modules/some-binary/package.json diff --git a/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/echo b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/echo deleted file mode 100755 index bc228dca41..0000000000 --- a/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/echo +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env node - -require("../echo"); diff --git a/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/some-binary b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/some-binary new file mode 100755 index 0000000000..83a3c57143 --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/some-binary @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require("../some-binary"); diff --git a/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/lib/index.js b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/some-binary/lib/index.js similarity index 100% rename from __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/lib/index.js rename to __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/some-binary/lib/index.js diff --git a/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/package.json b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/some-binary/package.json similarity index 100% rename from __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/package.json rename to __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/some-binary/package.json diff --git a/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/echo b/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/echo deleted file mode 100755 index bc228dca41..0000000000 --- a/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/echo +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env node - -require("../echo"); diff --git a/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/some-binary b/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/some-binary new file mode 100755 index 0000000000..83a3c57143 --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/some-binary @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require("../some-binary"); diff --git a/__tests__/fixtures/run/run-should-run-binary/node_modules/echo/lib/index.js b/__tests__/fixtures/run/run-should-run-binary/node_modules/some-binary/lib/index.js similarity index 100% rename from __tests__/fixtures/run/run-should-run-binary/node_modules/echo/lib/index.js rename to __tests__/fixtures/run/run-should-run-binary/node_modules/some-binary/lib/index.js diff --git a/__tests__/fixtures/run/run-should-run-binary/node_modules/echo/package.json b/__tests__/fixtures/run/run-should-run-binary/node_modules/some-binary/package.json similarity index 100% rename from __tests__/fixtures/run/run-should-run-binary/node_modules/echo/package.json rename to __tests__/fixtures/run/run-should-run-binary/node_modules/some-binary/package.json