import { CreateNodesContextV2, CreateNodesResultV2, PostTasksExecutionContext, ProjectConfiguration, TaskResult, serializeJson, } from '@nx/devkit'; import { TempFs } from './testing-utils/index'; import { WorkspacePluginOptions, createNodesV2, postTasksExecution } from './workspace-plugin'; import { PackageJson } from '../types'; const createTaskResult = (id: string, status: TaskResult['status']): TaskResult => ({ task: { id } as TaskResult['task'], status, code: status === 'failure' ? 1 : 0, }); const postTasksExecutionContext: PostTasksExecutionContext = { id: 'test-run', workspaceRoot: '/test', nxJsonConfiguration: {}, argv: [], startTime: 0, endTime: 1, taskResults: { 'successful-project:build': createTaskResult('successful-project:build', 'success'), 'failed-project:type-check': createTaskResult('failed-project:type-check', 'failure'), }, }; describe(`workspace-plugin`, () => { const [, createNodesFunction] = createNodesV2; const originalEnv = process.env; let context: CreateNodesContextV2; let tempFs: TempFs; let cwd: string; const options: WorkspacePluginOptions = { reactIntegrationTesting: { reactVersions: ['17', '18'], }, }; beforeEach(async () => { tempFs = new TempFs('test'); cwd = process.cwd(); process.chdir(tempFs.tempDir); process.env = { ...originalEnv }; delete process.env.GITHUB_ACTIONS; delete process.env.TF_BUILD; context = { nxJsonConfiguration: { namedInputs: { default: ['{projectRoot}/**/*'], production: ['!{projectRoot}/**/*.spec.ts'], }, }, workspaceRoot: tempFs.tempDir, }; }); afterEach(() => { process.env = originalEnv; jest.restoreAllMocks(); jest.resetModules(); tempFs.cleanup(); process.chdir(cwd); }); it('should report failed tasks as GitHub Actions errors', async () => { process.env.GITHUB_ACTIONS = 'true'; const consoleLog = jest.spyOn(console, 'log').mockImplementation(); await postTasksExecution(undefined, postTasksExecutionContext); expect(consoleLog).toHaveBeenCalledTimes(1); expect(consoleLog).toHaveBeenCalledWith('::error::Nx task failed: failed-project:type-check'); }); it('should report failed tasks as Azure Pipelines errors', async () => { process.env.TF_BUILD = 'true'; const consoleLog = jest.spyOn(console, 'log').mockImplementation(); await postTasksExecution(undefined, postTasksExecutionContext); expect(consoleLog).toHaveBeenCalledTimes(1); expect(consoleLog).toHaveBeenCalledWith('##vso[task.logissue type=error]Nx task failed: failed-project:type-check'); }); it('should not report failed tasks outside CI', async () => { const consoleLog = jest.spyOn(console, 'log').mockImplementation(); await postTasksExecution(undefined, postTasksExecutionContext); expect(consoleLog).not.toHaveBeenCalled(); }); it('should create nodes with clean,format targets for any project', async () => { await tempFs.createFiles({ 'proj/project.json': serializeJson({}), 'proj/package.json': serializeJson({}), }); const results = await createNodesFunction(['proj/project.json'], options, context); expect(getTargetsNames(results)).toEqual(['clean', 'format', 'type-check']); }); it('should add the test target when a jest.config.cjs (type:module) exists', async () => { await tempFs.createFiles({ 'proj/project.json': serializeJson({}), 'proj/package.json': serializeJson({ type: 'module' }), 'proj/jest.config.cjs': 'module.exports = {}', }); const results = await createNodesFunction(['proj/project.json'], options, context); expect(getTargetsNames(results)).toContain('test'); }); it('should add the attw target only for published esm first projects that declare exports', async () => { await tempFs.createFiles({ 'with-exports/project.json': serializeJson({ projectType: 'library', tags: ['vNext'] }), 'with-exports/package.json': serializeJson({ type: 'module', exports: { '.': './lib/index.js' } }), 'no-exports/project.json': serializeJson({ projectType: 'library', tags: ['vNext'] }), 'no-exports/package.json': serializeJson({ type: 'module' }), 'private-with-exports/project.json': serializeJson({ projectType: 'library', tags: ['vNext'] }), 'private-with-exports/package.json': serializeJson({ private: true, type: 'module', exports: { '.': './lib/index.js' }, }), 'commonjs-first/project.json': serializeJson({ projectType: 'library', tags: ['vNext'] }), 'commonjs-first/package.json': serializeJson({ exports: { '.': './lib-commonjs/index.js' } }), }); const withExports = await createNodesFunction(['with-exports/project.json'], options, context); const noExports = await createNodesFunction(['no-exports/project.json'], options, context); const privateWithExports = await createNodesFunction(['private-with-exports/project.json'], options, context); const commonjsFirst = await createNodesFunction(['commonjs-first/project.json'], options, context); expect(getTargetsNames(withExports, 'with-exports')).toContain('attw'); expect(getTargets(withExports, 'with-exports')?.attw).toMatchObject({ executor: 'nx:run-commands', dependsOn: ['build'], options: { cwd: 'with-exports', command: expect.stringContaining('attw --pack --profile node16') }, }); expect(getTargetsNames(noExports, 'no-exports')).not.toContain('attw'); expect(getTargetsNames(privateWithExports, 'private-with-exports')).not.toContain('attw'); // CommonJS-first packages always report inherent `CJS default export` interop findings expect(getTargetsNames(commonjsFirst, 'commonjs-first')).not.toContain('attw'); }); it('should add lint,test task only if configuration exists', async () => { await tempFs.createFiles({ 'proj/project.json': serializeJson({}), 'proj/package.json': serializeJson({}), 'proj/eslint.config.js': 'module.exports = {}', 'proj/jest.config.js': 'module.exports = {}', 'proj/monosize.config.mjs': 'export default {}', }); const results = await createNodesFunction(['proj/project.json'], options, context); const targets = getTargets(results); expect(targets?.lint).toMatchInlineSnapshot(` Object { "cache": true, "executor": "nx:run-commands", "inputs": Array [ "default", "{projectRoot}/eslint.{js,cjs,mjs}", "{workspaceRoot}/eslint.config.{js,cjs,mjs}", Object { "externalDependencies": Array [ "eslint", ], }, ], "metadata": Object { "description": "Runs ESLint on project", "help": Object { "command": "yarn run -T eslint --help", "example": Object { "options": Object { "max-warnings": 0, }, }, }, "technologies": Array [ "eslint", ], }, "options": Object { "command": "yarn run -T eslint src", "cwd": "proj", }, "outputs": Array [ "{options.outputFile}", ], } `); expect(targets?.test).toMatchInlineSnapshot(` Object { "cache": true, "command": "yarn run -T jest", "inputs": Array [ "default", "^production", "{workspaceRoot}/jest.preset.js", Object { "externalDependencies": Array [ "jest", ], }, ], "metadata": Object { "description": "Run Jest Tests", "help": Object { "command": "yarn run -T jest --help", "example": Object { "options": Object { "coverage": true, }, }, }, "technologies": Array [ "jest", ], }, "options": Object { "cwd": "proj", "passWithNoTests": true, }, "outputs": Array [ "{projectRoot}/coverage", ], } `); // bundle-size target should be added when monosize config exists expect(targets?.['bundle-size']).toMatchInlineSnapshot(` Object { "cache": true, "command": "yarn run -T monosize measure", "inputs": Array [ "{workspaceRoot}/monosize.config.mjs", "{projectRoot}/monosize.config.mjs", "{projectRoot}/bundle-size", "{projectRoot}/src/**/*.tsx?", Object { "externalDependencies": Array [ "monosize", "monosize-bundler-webpack", ], }, ], "metadata": Object { "help": Object { "command": "yarn run -T monosize measure --help", "example": Object {}, }, "technologies": Array [ "monosize", ], }, "options": Object { "cwd": "proj", }, "outputs": Array [ "{projectRoot}/dist/bundle-size", ], } `); }); it('should add the verify-bundle-isolation target only when a bundle-isolation.config.json exists', async () => { await tempFs.createFiles({ 'with-config/project.json': serializeJson({}), 'with-config/package.json': serializeJson({}), 'with-config/bundle-isolation.config.json': serializeJson({}), 'no-config/project.json': serializeJson({}), 'no-config/package.json': serializeJson({}), // a monosize fixtures dir alone must not be mistaken for an isolation opt-in 'no-config/bundle-size/A.fixture.js': '', }); const withConfig = await createNodesFunction(['with-config/project.json'], options, context); const noConfig = await createNodesFunction(['no-config/project.json'], options, context); expect(getTargetsNames(noConfig, 'no-config')).not.toContain('verify-bundle-isolation'); expect(getTargets(withConfig, 'with-config')?.['verify-bundle-isolation']).toEqual({ cache: true, dependsOn: ['build', '^build'], command: 'yarn run -T verify-bundle-isolation', options: { cwd: 'with-config' }, inputs: [ 'default', '^default', '{workspaceRoot}/tools/verify-bundle-isolation/**', { externalDependencies: ['ajv', 'webpack'] }, ], outputs: ['{projectRoot}/dist/bundle-isolation'], metadata: { technologies: ['webpack'], description: 'Assert entry points do not bundle runtimes the package is meant to stay free of', }, }); }); describe(`v9 project nodes`, () => { describe(`React Integration Tester config`, () => { it(`should not create atomized targets if project doesnt have required configurations`, async () => { await tempFs.createFiles({ 'proj/library/project.json': serializeJson({ root: 'proj', name: 'proj', projectType: 'library', tags: ['vNext'], } satisfies ProjectConfiguration), 'proj/library/package.json': serializeJson({ name: '@proj/proj', private: true, } satisfies Partial), }); const results = await createNodesFunction(['proj/library/project.json'], options, context); const targets = getTargets(results, 'proj/library')!; expect(targets['react-integration-testing'].dependsOn).toEqual([]); expect(targets['react-integration-testing--17']).toBeUndefined(); expect(targets['react-integration-testing--18']).toBeUndefined(); }); it(`should not create atomized targets if project is explicitly excluded via plugin config#exclude`, async () => { await tempFs.createFiles({ 'proj/library/project.json': serializeJson({ root: 'proj', name: 'proj', projectType: 'library', tags: ['vNext'], } satisfies ProjectConfiguration), 'proj/library/package.json': serializeJson({ name: '@proj/proj', private: true, } satisfies Partial), 'proj/stories/project.json': serializeJson({ root: 'proj/stories', name: 'proj-stories', tags: ['type:stories', 'vNext'], } satisfies ProjectConfiguration), }); const results = await createNodesFunction( ['proj/library/project.json'], { reactIntegrationTesting: { exclude: ['proj'], reactVersions: ['17', '18'] } }, context, ); const targets = getTargets(results, 'proj/library')!; expect(targets['react-integration-testing']).toEqual(undefined); expect(targets['react-integration-testing--17']).toBeUndefined(); expect(targets['react-integration-testing--18']).toBeUndefined(); }); it(`should create "--type-check" including "--prepare" atomized targets for "stories" projects`, async () => { await tempFs.createFiles({ 'proj/library/project.json': serializeJson({ root: 'proj', name: 'proj', projectType: 'library', tags: ['vNext'], } satisfies ProjectConfiguration), 'proj/library/package.json': serializeJson({ name: '@proj/proj', private: true, } satisfies Partial), 'proj/stories/project.json': serializeJson({ root: 'proj/stories', name: 'proj-stories', tags: ['type:stories', 'vNext'], } satisfies ProjectConfiguration), 'proj/stories/package.json': serializeJson({ name: '@proj/proj-stories', private: true, } satisfies Partial), }); const results = await createNodesFunction(['proj/stories/project.json'], options, context); const targets = getTargets(results, 'proj/stories')!; expect(targets['react-integration-testing--17--type-check']).toMatchInlineSnapshot(` Object { "cache": true, "command": "yarn run -T rit --project-id ci --react 17 --run type-check --verbose", "dependsOn": Array [ "react-integration-testing--17--prepare", ], "inputs": Array [ "default", "production", "^production", "{workspaceRoot}/jest.preset.js", "{workspaceRoot}/tools/react-integration-tester/**", ], "metadata": Object { "description": "Run react integration tests against React 17", "help": Object { "command": "yarn run -T rit --help", "example": Object {}, }, "technologies": Array [ "react-integration-tester", ], }, "options": Object { "cwd": "{projectRoot}", }, "outputs": Array [], } `); expect(targets['react-integration-testing--17--prepare']).toMatchInlineSnapshot(` Object { "cache": true, "command": "yarn run -T rit --prepare-only --no-install --project-id ci --react 17 --verbose", "dependsOn": Array [], "inputs": Array [ "default", "production", "^production", "{workspaceRoot}/jest.preset.js", "{workspaceRoot}/tools/react-integration-tester/**", ], "metadata": Object { "description": "Run react integration tests against React 17", "help": Object { "command": "yarn run -T rit --help", "example": Object {}, }, "technologies": Array [ "react-integration-tester", ], }, "options": Object { "cwd": "{projectRoot}", }, "outputs": Array [ "{workspaceRoot}/tmp/rit/react-17/proj-stories-react-17-ci", ], } `); }); it(`should --run type-check within targets for projects with "stories" sibling projects`, async () => { await tempFs.createFiles({ 'proj/library/project.json': serializeJson({ root: 'proj', name: 'proj', projectType: 'library', tags: ['vNext'], } satisfies ProjectConfiguration), 'proj/library/package.json': serializeJson({ name: '@proj/proj', private: true, } satisfies Partial), 'proj/stories/project.json': serializeJson({ root: 'proj/stories', name: 'proj-stories', tags: ['type:stories', 'vNext'], } satisfies ProjectConfiguration), }); const results = await createNodesFunction(['proj/library/project.json'], options, context); const targets = getTargets(results, 'proj/library')!; expect(targets['react-integration-testing--17--type-check']).toMatchInlineSnapshot(` Object { "cache": true, "dependsOn": Array [ Object { "projects": "proj-stories", "target": "react-integration-testing--17--type-check", }, ], "executor": "nx:noop", } `); expect(targets['react-integration-testing--17--prepare']).toBeUndefined(); }); it(`should --run e2e within targets for projects which have cypress config`, async () => { await tempFs.createFiles({ 'proj/library/project.json': serializeJson({ root: 'proj', name: 'proj', projectType: 'library', tags: ['vNext'], } satisfies ProjectConfiguration), 'proj/library/package.json': serializeJson({ name: '@proj/proj', private: true, } satisfies Partial), 'proj/library/cypress.config.ts': ` import { baseConfig } from '@proj/cypress'; export default baseConfig; `, }); const results = await createNodesFunction(['proj/library/project.json'], options, context); const targets = getTargets(results, 'proj/library')!; expect(targets['react-integration-testing--17--e2e'].command).toMatchInlineSnapshot( `"yarn run -T rit --project-id ci --react 17 --run e2e --verbose"`, ); expect(targets['react-integration-testing--17--prepare']).toBeDefined(); }); it(`should --run test within targets for projects which have jest config`, async () => { await tempFs.createFiles({ 'proj/library/project.json': serializeJson({ root: 'proj', name: 'proj', projectType: 'library', tags: ['vNext'], } satisfies ProjectConfiguration), 'proj/library/package.json': serializeJson({ name: '@proj/proj', private: true, } satisfies Partial), 'proj/library/jest.config.js': ` module.exports = {}; `, }); const results = await createNodesFunction(['proj/library/project.json'], options, context); const targets = getTargets(results, 'proj/library')!; expect(targets['react-integration-testing--17--test'].command).toMatchInlineSnapshot( `"yarn run -T rit --project-id ci --react 17 --run test --verbose"`, ); expect(targets['react-integration-testing--17--prepare']).toBeDefined(); }); it(`should create the targets name based on user provided options `, async () => { await tempFs.createFiles({ 'proj/library/project.json': serializeJson({ root: 'proj', name: 'proj', projectType: 'library', tags: ['vNext'], } satisfies ProjectConfiguration), 'proj/library/package.json': serializeJson({ name: '@proj/proj', private: true, } satisfies Partial), 'proj/library/jest.config.js': ` module.exports = {}; `, }); const results = await createNodesFunction( ['proj/library/project.json'], { reactIntegrationTesting: { reactVersions: ['17'], targetName: 'rit' } }, context, ); expect(getTargetsNames(results, 'proj/library')).toEqual(expect.arrayContaining(['rit', 'rit--17--test'])); }); }); it('should prefer rit.config.js when determining available rit run options', async () => { await tempFs.createFiles({ 'proj/library/project.json': serializeJson({ root: 'proj', name: 'proj', projectType: 'library', tags: ['vNext'], } satisfies ProjectConfiguration), 'proj/library/package.json': serializeJson({ name: '@proj/proj', private: true, } satisfies Partial), // provide a rit.config.js that points type-check to tsconfig.baz.json 'proj/library/rit.config.js': `module.exports = { react: { 17: { runConfig: { 'type-check': { configPath: 'tsconfig.baz.json' } } }, 18: { runConfig: { test: { configPath: 'jest-custom.config.js' } } } } }`, // create the referenced tsconfig so the logic sees it as present 'proj/library/cypress.config.ts': 'export default {}', 'proj/library/tsconfig.baz.json': '{}', // also create a jest.config.js to show that rit.config.js takes precedence 'proj/library/jest-custom.config.js': 'module.exports = {}', }); const results = await createNodesFunction(['proj/library/project.json'], options, context); const targets = getTargets(results, 'proj/library')!; expect(targets['react-integration-testing--17--e2e']).toBeDefined(); // Because rit.config.js explicitly references tsconfig.baz.json for type-check // the react-integration-testing--17--type-check target should be created. expect(targets['react-integration-testing--17--type-check']).toBeDefined(); // Ensure 'test' is not added because jest.config.js does not exists expect(targets['react-integration-testing--17--test']).toBeUndefined(); expect(targets['react-integration-testing--18--e2e']).toBeDefined(); // Ensure 'test' is added because rit.config.js is present and defines 'jest-custom.config.js' which exists expect(targets['react-integration-testing--18--test']).toBeDefined(); // Ensure 'test' is not added because tsconfig.lib.json does not exists expect(targets['react-integration-testing--18--type-check']).toBeUndefined(); }); it('should create default nodes for v9 library project having stories project sibling', async () => { await tempFs.createFiles({ 'proj/library/project.json': serializeJson({ root: 'proj', name: 'proj', projectType: 'library', tags: ['vNext'], } satisfies ProjectConfiguration), 'proj/library/package.json': serializeJson({ name: '@proj/proj', private: true, } satisfies Partial), 'proj/stories/project.json': serializeJson({ root: 'proj/stories', name: 'proj-stories', tags: ['type:stories', 'vNext'], } satisfies ProjectConfiguration), }); const results = await createNodesFunction(['proj/library/project.json'], options, context); expect(getTargetsNames(results, 'proj/library')).toEqual([ 'clean', 'format', 'type-check', 'generate-api', 'build', 'storybook', 'start', 'react-integration-testing--17--type-check', 'react-integration-testing--18--type-check', 'react-integration-testing', ]); expect(results).toMatchInlineSnapshot(` Array [ Array [ "proj/library/project.json", Object { "projects": Object { "proj/library": Object { "metadata": Object { "targetGroups": Object { "React Integration Tester": Array [ "react-integration-testing--17--type-check", "react-integration-testing--18--type-check", "react-integration-testing", ], }, }, "targets": Object { "build": Object { "cache": true, "executor": "@fluentui/workspace-plugin:build", "inputs": Array [ "{projectRoot}/package.json", "{projectRoot}/.swcrc", "{projectRoot}/config/api-extractor.json", "{projectRoot}/tsconfig.json", "{projectRoot}/tsconfig.lib.json", "{projectRoot}/src/**/*.tsx?", "{workspaceRoot}/scripts/api-extractor/api-extractor.*.json", Object { "externalDependencies": Array [ "@microsoft/api-extractor", "typescript", ], }, Object { "externalDependencies": Array [ "@swc/core", "@microsoft/api-extractor", "typescript", "babel-plugin-react-compiler", ], }, ], "metadata": Object { "help": Object { "command": "yarn run -T nx run proj:build --help", "example": Object {}, }, "technologies": Array [ "swc", "typescript", "api-extractor", ], }, "options": Object { "enableGriffelRawStyles": true, "moduleOutput": Array [ Object { "module": "es6", "outputPath": "lib", }, Object { "module": "commonjs", "outputPath": "lib-commonjs", }, ], "outputPathRoot": "{projectRoot}", "sourceRoot": "src", }, "outputs": Array [ "{projectRoot}/lib", "{projectRoot}/lib-commonjs", "{projectRoot}/dist", "{projectRoot}/etc/proj.api.md", ], }, "clean": Object { "executor": "@fluentui/workspace-plugin:clean", }, "format": Object { "cache": true, "command": "prettier --write {projectRoot}", "configurations": Object { "check": Object { "command": "prettier --check {projectRoot}", }, }, "inputs": Array [ "default", "{workspaceRoot}/.prettierignore", "{workspaceRoot}/prettier.config.js", "{workspaceRoot}/prettier.config.json", "{projectRoot}/**", ], "metadata": Object { "description": "Format code with prettier", "help": Object { "command": "yarn run -T prettier --help", "example": Object {}, }, "technologies": Array [ "prettier", ], }, }, "generate-api": Object { "cache": true, "executor": "@fluentui/workspace-plugin:generate-api", "inputs": Array [ "{projectRoot}/config/api-extractor.json", "{projectRoot}/tsconfig.json", "{projectRoot}/tsconfig.lib.json", "{projectRoot}/src/**/*.tsx?", "{workspaceRoot}/scripts/api-extractor/api-extractor.*.json", Object { "externalDependencies": Array [ "@microsoft/api-extractor", "typescript", ], }, ], "metadata": Object { "help": Object { "command": "yarn run -T nx run proj:generate-api --help", "example": Object {}, }, "technologies": Array [ "typescript", "api-extractor", ], }, "outputs": Array [ "{projectRoot}/dist/index.d.ts", "{projectRoot}/etc/proj.api.md", ], }, "react-integration-testing": Object { "cache": true, "dependsOn": Array [ Object { "params": "forward", "projects": "proj-stories", "target": "react-integration-testing--17--type-check", }, Object { "params": "forward", "projects": "proj-stories", "target": "react-integration-testing--18--type-check", }, ], "executor": "nx:noop", "inputs": Array [ "default", "production", "^production", "{workspaceRoot}/jest.preset.js", "{workspaceRoot}/tools/react-integration-tester/**", ], "metadata": Object { "description": "Run react integration tests against React 17, 18", "help": Object { "command": "yarn run -T rit --help", "example": Object {}, }, "technologies": Array [ "react-integration-tester", ], }, "outputs": Array [], }, "react-integration-testing--17--type-check": Object { "cache": true, "dependsOn": Array [ Object { "projects": "proj-stories", "target": "react-integration-testing--17--type-check", }, ], "executor": "nx:noop", }, "react-integration-testing--18--type-check": Object { "cache": true, "dependsOn": Array [ Object { "projects": "proj-stories", "target": "react-integration-testing--18--type-check", }, ], "executor": "nx:noop", }, "start": Object { "cache": true, "command": "nx run proj-stories:storybook", }, "storybook": Object { "cache": true, "command": "nx run proj-stories:storybook", }, "type-check": Object { "cache": true, "executor": "@fluentui/workspace-plugin:type-check", "inputs": Array [ "default", "{projectRoot}/tsconfig.json", "{projectRoot}/tsconfig*.json", Object { "externalDependencies": Array [ "typescript", ], }, ], "metadata": Object { "description": "Type check code with TypeScript", "technologies": Array [ "typescript", ], }, }, }, }, }, }, ], ] `); }); it('should create storybook targets for v9 stories project with .storybook/main.cjs', async () => { await tempFs.createFiles({ 'proj/stories/.storybook/main.cjs': '', 'proj/stories/project.json': serializeJson({ root: 'proj/stories', projectType: 'library', tags: ['vNext', 'type:stories'], } satisfies ProjectConfiguration), 'proj/stories/package.json': serializeJson({ name: '@proj/proj-stories', private: true, } satisfies Partial), }); const results = await createNodesFunction(['proj/stories/project.json'], options, context); expect(getTargetsNames(results, 'proj/stories')).toEqual(expect.arrayContaining(['storybook', 'start'])); }); it('should create default nodes for v9 stories project', async () => { await tempFs.createFiles({ 'proj/stories/.storybook/main.js': '', 'proj/stories/project.json': serializeJson({ root: 'proj/stories', projectType: 'library', tags: ['vNext', 'type:stories'], } satisfies ProjectConfiguration), 'proj/stories/package.json': serializeJson({ name: '@proj/proj-stories', private: true, } satisfies Partial), }); const results = await createNodesFunction(['proj/stories/project.json'], options, context); expect(getTargetsNames(results, 'proj/stories')).toEqual([ 'clean', 'format', 'type-check', 'storybook', 'test-ssr', 'start', 'react-integration-testing', ]); const targets = getTargets(results, 'proj/stories'); expect(targets?.storybook).toMatchInlineSnapshot(` Object { "cache": true, "command": "yarn run -T storybook dev", "inputs": Array [ "production", "{workspaceRoot}/.storybook/**", "{projectRoot}/.storybook/**", Object { "externalDependencies": Array [ "storybook", ], }, ], "metadata": Object { "help": Object { "command": "yarn run -T storybook dev --help", "example": Object {}, }, "technologies": Array [ "storybook", ], }, "options": Object { "cwd": "proj/stories", }, } `); expect(targets?.['test-ssr']).toMatchInlineSnapshot(` Object { "cache": true, "command": "yarn run -T test-ssr \\"./src/**/*.stories.tsx\\"", "metadata": Object { "help": Object { "command": "yarn run -T test-ssr --help", "example": Object {}, }, "technologies": Array [ "test-ssr", ], }, "options": Object { "cwd": "proj/stories", }, } `); }); describe('generate-api target', () => { const v9LibFiles = { 'proj/project.json': serializeJson({ root: 'proj', name: 'proj', projectType: 'library', tags: ['vNext'], } satisfies ProjectConfiguration), 'proj/package.json': serializeJson({ name: '@proj/proj', private: true, } satisfies Partial), }; async function setupAndGetGenerateApiTarget(extraFiles: Record = {}) { await tempFs.createFiles({ ...v9LibFiles, ...extraFiles }); const results = await createNodesFunction(['proj/project.json'], options, context); return getTargets(results)?.['generate-api']; } it('should use default inputs and outputs for a standard v9 library', async () => { const target = await setupAndGetGenerateApiTarget(); expect(target?.inputs).toEqual([ '{projectRoot}/config/api-extractor.json', '{projectRoot}/tsconfig.json', '{projectRoot}/tsconfig.lib.json', '{projectRoot}/src/**/*.tsx?', '{workspaceRoot}/scripts/api-extractor/api-extractor.*.json', { externalDependencies: ['@microsoft/api-extractor', 'typescript'] }, ]); expect(target?.outputs).toEqual(['{projectRoot}/dist/index.d.ts', '{projectRoot}/etc/proj.api.md']); }); it('should add glob outputs when user configures exportSubpaths on generate-api target', async () => { const extraFiles = { 'proj/project.json': serializeJson({ root: 'proj', name: 'proj', projectType: 'library', tags: ['vNext'], targets: { 'generate-api': { options: { exportSubpaths: true, }, }, }, } satisfies ProjectConfiguration), }; await tempFs.createFiles({ ...v9LibFiles, ...extraFiles }); const results = await createNodesFunction(['proj/project.json'], options, context); const targets = getTargets(results); const generateApiTarget = targets?.['generate-api']; expect(generateApiTarget?.outputs).toEqual(['{projectRoot}/dist/**/*.d.ts', '{projectRoot}/etc/*.api.md']); const buildTarget = targets?.build; expect(buildTarget?.options?.generateApi).toEqual({ exportSubpaths: true }); expect(buildTarget?.outputs).toEqual([ '{projectRoot}/lib', '{projectRoot}/lib-commonjs', '{projectRoot}/dist', '{projectRoot}/etc/*.api.md', ]); }); }); it('should add verify-packaging task only if package is not private', async () => { await tempFs.createFiles({ 'proj/project.json': serializeJson({ root: 'proj', projectType: 'library', tags: ['vNext'], } satisfies ProjectConfiguration), 'proj/package.json': serializeJson({ name: '@proj/proj' } satisfies Partial), }); const results = await createNodesFunction(['proj/project.json'], options, context); const targets = getTargets(results); expect(targets?.['verify-packaging']).toMatchInlineSnapshot(` Object { "executor": "@fluentui/workspace-plugin:verify-packaging", } `); }); describe('react library project', () => { describe('with react in peerDependencies', () => { let targets: ReturnType; let metadata: ReturnType; beforeEach(async () => { await tempFs.createFiles({ 'proj/library/project.json': serializeJson({ root: 'proj/library', name: 'proj', projectType: 'library', tags: ['vNext'], } satisfies ProjectConfiguration), 'proj/library/package.json': serializeJson({ name: '@proj/proj', peerDependencies: { react: '>=16' }, } satisfies Partial), }); const results = await createNodesFunction(['proj/library/project.json'], options, context); targets = getTargets(results, 'proj/library'); metadata = getMetadata(results, 'proj/library'); }); it.each(['react-compiler-analyzer--lint', 'react-compiler-analyzer--analyze'])( 'should build the workspace analyzer before %s', targetName => { expect(targets?.[targetName].dependsOn).toEqual([{ target: 'build', projects: 'react-compiler-analyzer' }]); }, ); it.each(['react-compiler-analyzer--lint', 'react-compiler-analyzer--analyze', 'react-compiler-analyzer'])( 'should invalidate %s when the workspace analyzer changes', targetName => { expect(targets?.[targetName].inputs).toEqual( expect.arrayContaining([ '{workspaceRoot}/tools/react-compiler-analyzer/src/**/*', '{workspaceRoot}/tools/react-compiler-analyzer/bin/**/*', '{workspaceRoot}/tools/react-compiler-analyzer/esbuild.config.mjs', '{workspaceRoot}/tools/react-compiler-analyzer/package.json', '{workspaceRoot}/tools/react-compiler-analyzer/project.json', '{workspaceRoot}/tools/react-compiler-analyzer/rca*.schema.json', ]), ); }, ); it('should add react-compiler-analyzer target group', async () => { expect(targets?.['react-compiler-analyzer--lint']).toMatchInlineSnapshot(` Object { "cache": true, "command": "yarn run -T react-compiler-analyzer lint ./src", "dependsOn": Array [ Object { "projects": "react-compiler-analyzer", "target": "build", }, ], "inputs": Array [ "default", "{workspaceRoot}/tools/react-compiler-analyzer/src/**/*", "{workspaceRoot}/tools/react-compiler-analyzer/bin/**/*", "{workspaceRoot}/tools/react-compiler-analyzer/esbuild.config.mjs", "{workspaceRoot}/tools/react-compiler-analyzer/package.json", "{workspaceRoot}/tools/react-compiler-analyzer/project.json", "{workspaceRoot}/tools/react-compiler-analyzer/rca*.schema.json", Object { "externalDependencies": Array [ "babel-plugin-react-compiler", ], }, ], "metadata": Object { "description": "Lint redundant 'use no memo' directives", "help": Object { "command": "yarn run -T react-compiler-analyzer lint --help", "example": Object { "options": Object { "fix": true, }, }, }, "technologies": Array [ "react-compiler", ], }, "options": Object { "cwd": "proj/library", }, } `); expect(targets?.['react-compiler-analyzer--analyze']).toMatchInlineSnapshot(` Object { "cache": true, "command": "yarn run -T react-compiler-analyzer analyze ./src", "dependsOn": Array [ Object { "projects": "react-compiler-analyzer", "target": "build", }, ], "inputs": Array [ "default", "{workspaceRoot}/tools/react-compiler-analyzer/src/**/*", "{workspaceRoot}/tools/react-compiler-analyzer/bin/**/*", "{workspaceRoot}/tools/react-compiler-analyzer/esbuild.config.mjs", "{workspaceRoot}/tools/react-compiler-analyzer/package.json", "{workspaceRoot}/tools/react-compiler-analyzer/project.json", "{workspaceRoot}/tools/react-compiler-analyzer/rca*.schema.json", Object { "externalDependencies": Array [ "babel-plugin-react-compiler", ], }, ], "metadata": Object { "description": "Analyze React Compiler coverage and migration status", "help": Object { "command": "yarn run -T react-compiler-analyzer analyze --help", "example": Object { "options": Object { "annotate": true, }, }, }, "technologies": Array [ "react-compiler", ], }, "options": Object { "cwd": "proj/library", }, } `); expect(targets?.['react-compiler-analyzer']).toMatchInlineSnapshot(` Object { "cache": true, "dependsOn": Array [ "react-compiler-analyzer--lint", ], "executor": "nx:noop", "inputs": Array [ "default", "{workspaceRoot}/tools/react-compiler-analyzer/src/**/*", "{workspaceRoot}/tools/react-compiler-analyzer/bin/**/*", "{workspaceRoot}/tools/react-compiler-analyzer/esbuild.config.mjs", "{workspaceRoot}/tools/react-compiler-analyzer/package.json", "{workspaceRoot}/tools/react-compiler-analyzer/project.json", "{workspaceRoot}/tools/react-compiler-analyzer/rca*.schema.json", Object { "externalDependencies": Array [ "babel-plugin-react-compiler", ], }, ], "metadata": Object { "description": "React Compiler analysis (runs lint on CI)", "help": Object { "command": "yarn run -T react-compiler-analyzer --help", "example": Object {}, }, "technologies": Array [ "react-compiler", ], }, } `); expect(metadata?.targetGroups).toMatchInlineSnapshot(` Object { "React Compiler Analyzer": Array [ "react-compiler-analyzer--lint", "react-compiler-analyzer--analyze", "react-compiler-analyzer", ], "React Integration Tester": Array [ "react-integration-testing", ], } `); }); it('should not set reactCompiler build option by default', async () => { expect(targets?.build.options.reactCompiler).toBeUndefined(); }); }); describe('without react in peerDependencies', () => { let targets: ReturnType; beforeEach(async () => { await tempFs.createFiles({ 'proj/library/project.json': serializeJson({ root: 'proj/library', name: 'proj', projectType: 'library', tags: ['vNext'], } satisfies ProjectConfiguration), 'proj/library/package.json': serializeJson({ name: '@proj/proj', private: true, } satisfies Partial), }); const results = await createNodesFunction(['proj/library/project.json'], options, context); targets = getTargets(results, 'proj/library'); }); it('should not add react-compiler-analyzer targets', async () => { expect(targets?.['react-compiler-analyzer']).toBeUndefined(); expect(targets?.['react-compiler-analyzer--lint']).toBeUndefined(); expect(targets?.['react-compiler-analyzer--analyze']).toBeUndefined(); }); it('should not set reactCompiler build option', async () => { expect(targets?.build.options.reactCompiler).toBeUndefined(); }); }); }); }); }); function getTargets(results: CreateNodesResultV2, projRoot = 'proj') { return results[0][1].projects?.[projRoot].targets; } function getMetadata(results: CreateNodesResultV2, projRoot = 'proj') { return results[0][1].projects?.[projRoot].metadata; } function getTargetsNames(results: CreateNodesResultV2, projRoot = 'proj'): string[] { return Object.keys(getTargets(results, projRoot) ?? {}); }