Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(run): only defer to Nx when targetDefaults are defined in nx.json
- Loading branch information
1 parent
d68f19a
commit 127f90c
Showing
11 changed files
with
169 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/run/src/__fixtures__/powered-by-nx-with-target-defaults/lerna.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"version": "1.0.0", | ||
"useNx": true | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/run/src/__fixtures__/powered-by-nx-with-target-defaults/nx.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"tasksRunnerOptions": { | ||
"default": { | ||
"runner": "nx/tasks-runners/default", | ||
"options": { | ||
"useDaemonProcess": false, | ||
"cacheableOperations": ["my-cacheable-script"] | ||
} | ||
} | ||
}, | ||
"targetDefaults": { | ||
"build": { | ||
"dependsOn": ["^build", "prebuild"] | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/run/src/__fixtures__/powered-by-nx-with-target-defaults/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "powered-by-nx" | ||
} |
9 changes: 9 additions & 0 deletions
9
...s/run/src/__fixtures__/powered-by-nx-with-target-defaults/packages/package-1/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "package-1", | ||
"version": "1.0.0", | ||
"scripts": { | ||
"fail": "exit 1", | ||
"my-script": "echo package-1", | ||
"another-script:but-with-colons": "echo package-1-script-with-colons" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...s/run/src/__fixtures__/powered-by-nx-with-target-defaults/packages/package-2/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "package-2", | ||
"version": "1.0.0", | ||
"scripts": { | ||
"fail": "exit 1" | ||
}, | ||
"dependencies": { | ||
"package-1": "^1.0.0" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...s/run/src/__fixtures__/powered-by-nx-with-target-defaults/packages/package-3/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "package-3", | ||
"version": "1.0.0", | ||
"scripts": { | ||
"my-script": "echo package-3" | ||
}, | ||
"devDependencies": { | ||
"package-2": "^1.0.0" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...s/run/src/__fixtures__/powered-by-nx-with-target-defaults/packages/package-4/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "package-4", | ||
"version": "1.0.0", | ||
"scripts": { | ||
"my-cacheable-script": "echo cacheable" | ||
}, | ||
"dependencies": { | ||
"package-1": "^0.0.0" | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
packages/run/src/__tests__/run-command-with-nx-no-target-defaults.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
jest.mock('../lib/npm-run-script'); | ||
|
||
jest.mock('@lerna-lite/core', () => ({ | ||
...(jest.requireActual('@lerna-lite/core') as any), // return the other real methods, below we'll mock only 2 of the methods | ||
logOutput: jest.requireActual('../../../core/src/__mocks__/output').logOutput, | ||
runTopologically: jest.requireActual('../../../core/src/utils/run-topologically').runTopologically, | ||
QueryGraph: jest.requireActual('../../../core/src/utils/query-graph').QueryGraph, | ||
})); | ||
|
||
// also point to the local run command so that all mocks are properly used even by the command-runner | ||
jest.mock('@lerna-lite/run', () => jest.requireActual('../run-command')); | ||
|
||
// mocked modules | ||
import { npmRunScript, npmRunScriptStreaming } from '../lib/npm-run-script'; | ||
import cliRunCommands from '../../../cli/src/cli-commands/cli-run-commands'; | ||
|
||
// helpers | ||
import { commandRunner, initFixtureFactory, loggingOutput, normalizeRelativeDir } from '@lerna-test/helpers'; | ||
const lernaRun = commandRunner(cliRunCommands); | ||
const initFixture = initFixtureFactory(__dirname); | ||
|
||
describe('RunCommand', () => { | ||
(npmRunScript as jest.Mock).mockImplementation((script, { pkg }) => | ||
Promise.resolve({ exitCode: 0, stdout: pkg.name }) | ||
); | ||
(npmRunScriptStreaming as jest.Mock).mockImplementation(() => Promise.resolve({ exitCode: 0 })); | ||
|
||
afterEach(() => { | ||
process.exitCode = undefined; | ||
}); | ||
|
||
// this is a temporary set of tests, which will be replaced by verdacio-driven tests | ||
// once the required setup is fully set up | ||
describe('in a repo powered by Nx', () => { | ||
let testDir; | ||
let collectedOutput = ''; | ||
let originalStdout; | ||
|
||
beforeAll(async () => { | ||
testDir = await initFixture('powered-by-nx'); | ||
process.env.NX_WORKSPACE_ROOT_PATH = testDir; | ||
// @ts-ignore | ||
jest.spyOn(process, 'exit').mockImplementation((code: any) => { | ||
if (code !== 0) { | ||
throw new Error(); | ||
} | ||
}); | ||
originalStdout = process.stdout.write; | ||
(process.stdout as any).write = (v) => { | ||
collectedOutput = `${collectedOutput}\n${v}`; | ||
}; | ||
}); | ||
|
||
afterAll(() => { | ||
process.stdout.write = originalStdout; | ||
}); | ||
|
||
it('runs a script in packages', async () => { | ||
collectedOutput = ''; | ||
await lernaRun(testDir)('my-script'); | ||
|
||
expect(collectedOutput).toContain('package-1'); | ||
expect(collectedOutput).toContain('package-3'); | ||
expect(collectedOutput).toContain('Successfully ran target'); | ||
|
||
const logMessages = loggingOutput('verbose'); | ||
expect(logMessages).toContain( | ||
'nx.json was not found or is missing targetDefaults. Task dependencies will not be automatically included.' | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters