Skip to content

Commit

Permalink
fix(misc): update e2e config generators to align with new TS solution…
Browse files Browse the repository at this point in the history
… setup
  • Loading branch information
jaysoo committed Jan 16, 2025
1 parent 8a6e44b commit d55ad0e
Show file tree
Hide file tree
Showing 17 changed files with 464 additions and 24 deletions.
19 changes: 15 additions & 4 deletions packages/cypress/src/generators/base-setup/base-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,25 @@ export function addBaseCypressSetup(
}

const opts = normalizeOptions(tree, projectConfig, options);
const isUsingTsSolutionConfig = isUsingTsSolutionSetup(tree);
const templateVars = {
...opts,
jsx: !!opts.jsx,
offsetFromRoot: offsetFromRoot(projectConfig.root),
offsetFromProjectRoot: opts.hasTsConfig ? opts.offsetFromProjectRoot : '',
tsConfigPath: opts.hasTsConfig
? `${opts.offsetFromProjectRoot}tsconfig.json`
: getRelativePathToRootTsConfig(tree, projectConfig.root),
tsConfigPath:
// TS solution setup should always extend from tsconfig.base.json to use shared compilerOptions, the project's tsconfig.json will not have compilerOptions.
isUsingTsSolutionConfig
? getRelativePathToRootTsConfig(
tree,
opts.hasTsConfig
? joinPathFragments(projectConfig.root, options.directory)
: // If an existing tsconfig.json file does not exist, then cypress tsconfig will be moved to the project root.
projectConfig.root
)
: opts.hasTsConfig
? `${opts.offsetFromProjectRoot}tsconfig.json`
: getRelativePathToRootTsConfig(tree, projectConfig.root),
linter: isEslintInstalled(tree) ? 'eslint' : 'none',
ext: '',
};
Expand All @@ -58,7 +69,7 @@ export function addBaseCypressSetup(

generateFiles(
tree,
isUsingTsSolutionSetup(tree)
isUsingTsSolutionConfig
? join(__dirname, 'files/tsconfig/ts-solution')
: join(__dirname, 'files/tsconfig/non-ts-solution'),
projectConfig.root,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Tree,
updateJson,
updateProjectConfiguration,
writeJson,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import cypressE2EConfigurationGenerator from './configuration';
Expand Down Expand Up @@ -555,6 +556,77 @@ export default defineConfig({
"
`);
});

describe('TS Solution Setup', () => {
beforeEach(() => {
updateJson(tree, 'package.json', (json) => {
json.workspaces = ['packages/*', 'apps/*'];
return json;
});
writeJson(tree, 'tsconfig.base.json', {
compilerOptions: {
composite: true,
declaration: true,
},
});
writeJson(tree, 'tsconfig.json', {
extends: './tsconfig.base.json',
files: [],
references: [],
});
});

it('should handle existing tsconfig.json files', async () => {
addProject(tree, { name: 'my-lib', type: 'libs' });
writeJson(tree, 'libs/my-lib/tsconfig.json', {
include: [],
files: [],
references: [],
});

await cypressE2EConfigurationGenerator(tree, {
project: 'my-lib',
baseUrl: 'http://localhost:4200',
js: true,
});

expect(tree.read('libs/my-lib/tsconfig.json', 'utf-8'))
.toMatchInlineSnapshot(`
"{
"include": [],
"files": [],
"references": [
{
"path": "./src/tsconfig.json"
}
]
}
"
`);
expect(tree.read('libs/my-lib/src/tsconfig.json', 'utf-8'))
.toMatchInlineSnapshot(`
"{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "out-tsc/cypress",
"allowJs": true,
"types": ["cypress", "node"],
"sourceMap": false
},
"include": [
"**/*.ts",
"**/*.js",
"../cypress.config.ts",
"../**/*.cy.ts",
"../**/*.cy.js",
"../**/*.d.ts"
],
"exclude": ["out-tsc", "test-output"]
}
"
`);
});
});
});
});

Expand Down
152 changes: 148 additions & 4 deletions packages/detox/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
readJson,
readProjectConfiguration,
Tree,
updateJson,
writeJson,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Linter } from '@nx/eslint/src/generators/utils/linter';
Expand Down Expand Up @@ -396,8 +398,38 @@ describe('detox application generator', () => {
addPlugin: true,
});

const tsConfig = readJson(tree, 'my-app-e2e/tsconfig.json');
expect(tsConfig.extends).toEqual('../tsconfig.base.json');
expect(readJson(tree, 'my-app-e2e/tsconfig.json')).toMatchInlineSnapshot(`
{
"extends": "../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.e2e.json",
},
],
}
`);
expect(readJson(tree, 'my-app-e2e/tsconfig.e2e.json'))
.toMatchInlineSnapshot(`
{
"compilerOptions": {
"allowJs": true,
"outDir": "../dist/out-tsc",
"sourceMap": false,
"types": [
"node",
"jest",
"detox",
],
},
"extends": "./tsconfig.json",
"include": [
"src/**/*.ts",
"src/**/*.js",
],
}
`);
});

it('should support a root tsconfig.json instead of tsconfig.base.json', async () => {
Expand All @@ -411,8 +443,120 @@ describe('detox application generator', () => {
addPlugin: true,
});

const tsConfig = readJson(tree, 'my-app-e2e/tsconfig.json');
expect(tsConfig.extends).toEqual('../tsconfig.json');
expect(readJson(tree, 'my-app-e2e/tsconfig.json')).toMatchInlineSnapshot(`
{
"extends": "../tsconfig.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.e2e.json",
},
],
}
`);
expect(readJson(tree, 'my-app-e2e/tsconfig.e2e.json'))
.toMatchInlineSnapshot(`
{
"compilerOptions": {
"allowJs": true,
"outDir": "../dist/out-tsc",
"sourceMap": false,
"types": [
"node",
"jest",
"detox",
],
},
"extends": "./tsconfig.json",
"include": [
"src/**/*.ts",
"src/**/*.js",
],
}
`);
});
});

describe('TS Solution Setup', () => {
beforeEach(() => {
updateJson(tree, 'package.json', (json) => {
json.workspaces = ['packages/*', 'apps/*'];
return json;
});
writeJson(tree, 'tsconfig.base.json', {
compilerOptions: {
composite: true,
declaration: true,
},
});
writeJson(tree, 'tsconfig.json', {
extends: './tsconfig.base.json',
files: [],
references: [],
});
});

it('should create tsconfig.json and update project references', async () => {
writeJson(tree, 'apps/my-app/package.json', {
name: 'my-app',
});

await detoxApplicationGenerator(tree, {
e2eDirectory: 'apps/my-app-e2e',
appProject: 'my-app',
linter: Linter.None,
framework: 'react-native',
addPlugin: true,
});

expect(tree.read('tsconfig.json', 'utf-8')).toMatchInlineSnapshot(`
"{
"extends": "./tsconfig.base.json",
"files": [],
"references": [
{
"path": "./apps/my-app-e2e"
}
]
}
"
`);
expect(tree.read('apps/my-app-e2e/package.json', 'utf-8'))
.toMatchInlineSnapshot(`
"{
"name": "my-app-e2e",
"version": "0.0.1",
"private": true,
"nx": {
"sourceRoot": "apps/my-app-e2e/src",
"projectType": "application",
"implicitDependencies": [
"my-app"
]
}
}
"
`);
expect(tree.read('apps/my-app-e2e/tsconfig.json', 'utf-8'))
.toMatchInlineSnapshot(`
"{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"sourceMap": false,
"outDir": "out-tsc/detox",
"allowJs": true,
"types": ["node", "jest", "detox"],
"rootDir": "src",
"module": "esnext",
"moduleResolution": "bundler",
"tsBuildInfoFile": "out-tsc/detox/tsconfig.tsbuildinfo"
},
"include": ["src/**/*.ts", "src/**/*.js"],
"exclude": ["out-tsc", "dist", "test-output"]
}
"
`);
});
});
});
27 changes: 27 additions & 0 deletions packages/detox/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { createFiles } from './lib/create-files';
import { normalizeOptions } from './lib/normalize-options';
import { Schema } from './schema';
import { ensureDependencies } from './lib/ensure-dependencies';
import {
addProjectToTsSolutionWorkspace,
updateTsconfigFiles,
} from '@nx/js/src/utils/typescript/ts-solution-setup';
import { sortPackageJsonFields } from '@nx/js/src/utils/package-json/sort-fields';

export async function detoxApplicationGenerator(host: Tree, schema: Schema) {
return await detoxApplicationGeneratorInternal(host, {
Expand Down Expand Up @@ -38,6 +43,28 @@ export async function detoxApplicationGeneratorInternal(
const lintingTask = await addLinting(host, options);
const depsTask = ensureDependencies(host, options);

updateTsconfigFiles(
host,
options.e2eProjectRoot,
'tsconfig.json',
{
module: 'esnext',
moduleResolution: 'bundler',
outDir: 'out-tsc/detox',
allowJs: true,
types: ['node', 'jest', 'detox'],
},
options.linter === 'eslint'
? ['eslint.config.js', 'eslint.config.cjs', 'eslint.config.mjs']
: undefined
);

if (options.isUsingTsSolutionConfig) {
addProjectToTsSolutionWorkspace(host, options.e2eProjectRoot);
}

sortPackageJsonFields(host, options.e2eProjectRoot);

if (!options.skipFormat) {
await formatFiles(host);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('Add Linting', () => {
appExpoName: 'MyApp',
appRoot: 'apps/my-app',
linter: Linter.EsLint,
isUsingTsSolutionConfig: false,
framework: 'react-native',
});
});
Expand All @@ -36,6 +37,7 @@ describe('Add Linting', () => {
appExpoName: 'MyApp',
appRoot: 'apps/my-app',
linter: Linter.EsLint,
isUsingTsSolutionConfig: false,
framework: 'react-native',
});

Expand All @@ -54,6 +56,7 @@ describe('Add Linting', () => {
appExpoName: 'MyApp',
appRoot: 'apps/my-app',
linter: Linter.None,
isUsingTsSolutionConfig: false,
framework: 'react-native',
});
const project = readProjectConfiguration(tree, 'my-app-e2e');
Expand Down
Loading

0 comments on commit d55ad0e

Please sign in to comment.