-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nx-plugin): update e2e tests to use local-registry and be simpler
- Loading branch information
1 parent
909d973
commit 9209019
Showing
6 changed files
with
168 additions
and
57 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
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
75 changes: 51 additions & 24 deletions
75
packages/plugin/src/generators/create-package/files/e2e/__cliName__.spec.ts__tmpl__
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 |
---|---|---|
@@ -1,31 +1,58 @@ | ||
import { | ||
checkFilesExist, | ||
removeTmpProject, | ||
tmpFolder, | ||
uniq, | ||
} from '@nx/plugin/testing'; | ||
import { execSync } from 'child_process'; | ||
import { join } from 'path'; | ||
|
||
describe('<%= cliName %> e2e', () => { | ||
let project: string; | ||
beforeAll(async () => { | ||
// create a workspace with cli | ||
project = uniq('<%= cliName %>'); | ||
execSync(`npx <%= cliName %>@1.0.0 ${project}`, { | ||
cwd: tmpFolder(), | ||
env: process.env, | ||
}); | ||
}, 240_000); | ||
import { join, dirname } from 'path'; | ||
import { mkdirSync, rmSync } from 'fs'; | ||
|
||
describe('<%= cliName %>', () => { | ||
let projectDirectory: string; | ||
|
||
beforeAll(() => { | ||
projectDirectory = createTestProject(); | ||
}); | ||
|
||
afterAll(() => { | ||
// Remove the generated project from the file system | ||
removeTmpProject(project); | ||
// Cleanup the test project | ||
rmSync(projectDirectory, { | ||
recursive: true, | ||
force: true, | ||
}); | ||
}); | ||
|
||
it('should create project using <%= cliName %>', () => { | ||
expect(() => | ||
checkFilesExist(join(tmpFolder(), project, 'package.json')) | ||
).not.toThrow(); | ||
|
||
it('should be installed', () => { | ||
// npm ls will fail if the package is not installed properly | ||
execSync('<%= packageManagerCommands.list %> <%= pluginPackageName %>', { | ||
cwd: projectDirectory, | ||
stdio: 'inherit', | ||
}); | ||
}); | ||
}); | ||
|
||
/** | ||
* Creates a test project with create-nx-workspace and installs the plugin | ||
* @returns The directory where the test project was created | ||
*/ | ||
function createTestProject() { | ||
const projectName = 'test-project'; | ||
const projectDirectory = join(process.cwd(), 'tmp', projectName); | ||
|
||
// Ensure projectDirectory is empty | ||
rmSync(projectDirectory, { | ||
recursive: true, | ||
force: true, | ||
}); | ||
mkdirSync(dirname(projectDirectory), { | ||
recursive: true, | ||
}); | ||
|
||
execSync( | ||
`<%= packageManagerCommands.exec %> --yes <%= cliName %>@e2e ${projectName} --no-interactive`, | ||
{ | ||
cwd: dirname(projectDirectory), | ||
stdio: 'inherit', | ||
env: process.env, | ||
} | ||
); | ||
console.log(`Created test project in "${projectDirectory}"`); | ||
|
||
return projectDirectory; | ||
} |
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
85 changes: 58 additions & 27 deletions
85
packages/plugin/src/generators/e2e-project/files/tests/__pluginName__.spec.ts__tmpl__
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 |
---|---|---|
@@ -1,35 +1,66 @@ | ||
import { | ||
checkFilesExist, | ||
ensureNxProject, | ||
runNxCommandAsync, | ||
runNxCommand, | ||
} from '@nx/plugin/testing'; | ||
|
||
describe('<%= pluginName %> e2e', () => { | ||
// Setting up individual workspaces per | ||
// test can cause e2e runs to take a long time. | ||
// For this reason, we recommend each suite only | ||
// consumes 1 workspace. The tests should each operate | ||
// on a unique project in the workspace, such that they | ||
// are not dependant on one another. | ||
import { execSync } from 'child_process'; | ||
import { join, dirname } from 'path'; | ||
import { mkdirSync, rmSync } from 'fs'; | ||
|
||
describe('<%= pluginName %>', () => { | ||
let projectDirectory: string; | ||
|
||
beforeAll(() => { | ||
ensureNxProject('<%= npmPackageName %>', '<%= pluginOutputPath %>'); | ||
projectDirectory = createTestProject(); | ||
}); | ||
|
||
afterAll(async () => { | ||
// `nx reset` kills the daemon, and performs | ||
// some work which can help clean up e2e leftovers | ||
await runNxCommandAsync('reset'); | ||
afterAll(() => { | ||
// Cleanup the test project | ||
rmSync(projectDirectory, { | ||
recursive: true, | ||
force: true, | ||
}); | ||
}); | ||
|
||
|
||
// Add some tests here to check that your plugin functionality works as expected. | ||
// A sample test is included below to give you some ideas. | ||
xit('should be able to build generated projects', async () => { | ||
const name = 'proj'; | ||
const generator = 'PLACEHOLDER'; | ||
await runNxCommandAsync(`generate <%= npmPackageName %>:${generator} --name ${name}`); | ||
expect(() => runNxCommand('build ${proj}')).not.toThrow(); | ||
expect(() => checkFilesExist(`dist/${name}/index.js`)).not.toThrow(); | ||
it('should be installed', () => { | ||
// npm ls will fail if the package is not installed properly | ||
execSync('<%= packageManagerCommands.list %> <%= pluginPackageName %>', { | ||
cwd: projectDirectory, | ||
stdio: 'inherit', | ||
}); | ||
}); | ||
}); | ||
|
||
/** | ||
* Creates a test project with create-nx-workspace and installs the plugin | ||
* @returns The directory where the test project was created | ||
*/ | ||
function createTestProject() { | ||
const projectName = 'test-project'; | ||
const projectDirectory = join(process.cwd(), 'tmp', projectName); | ||
|
||
// Ensure projectDirectory is empty | ||
rmSync(projectDirectory, { | ||
recursive: true, | ||
force: true, | ||
}); | ||
mkdirSync(dirname(projectDirectory), { | ||
recursive: true, | ||
}); | ||
|
||
execSync( | ||
`<%= packageManagerCommands.exec %> --yes create-nx-workspace@latest ${projectName} --preset empty --no-nxCloud --no-interactive`, | ||
{ | ||
cwd: dirname(projectDirectory), | ||
stdio: 'inherit', | ||
env: process.env, | ||
} | ||
); | ||
console.log(`Created test project in "${projectDirectory}"`); | ||
|
||
// The plugin has been built and published to a local registry in the jest globalSetup | ||
// Install the plugin built with the latest source code into the test repo | ||
execSync(`<%= packageManagerCommands.install %> <%= pluginPackageName %>@e2e`, { | ||
cwd: projectDirectory, | ||
stdio: 'inherit', | ||
env: process.env, | ||
}); | ||
|
||
return projectDirectory; | ||
} |