/
angularHandler.spec.ts
175 lines (137 loc) · 5.6 KB
/
angularHandler.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import chai, { expect } from 'chai'
import chaiPromise from 'chai-as-promised'
import * as fs from 'fs-extra'
import cloneDeep from 'lodash/cloneDeep'
import * as path from 'path'
import { WebpackDevServerConfig } from '../../src/devServer'
import {
angularHandler,
AngularJsonProjectConfig,
generateTsConfig,
getAngularBuildOptions,
getAngularCliModules,
getAngularJson,
getTempDir,
toPosix,
} from '../../src/helpers/angularHandler'
import '../support'
import { scaffoldMigrationProject } from '../test-helpers/scaffoldProject'
chai.use(chaiPromise)
const projectConfig: AngularJsonProjectConfig = {
root: 'my-root',
sourceRoot: 'my-root/src',
projectType: 'application',
architect: {
build: {
options: {
aot: true,
tsConfig: 'tsconfig.json',
polyfills: 'src/polyfills.ts',
optimization: true,
},
configurations: {
development: {
optimization: false,
},
},
},
},
}
describe('angularHandler', function () {
this.timeout(1000 * 60)
it('sources the config from angular-13', async () => {
const projectRoot = await scaffoldMigrationProject('angular-13')
process.chdir(projectRoot)
const devServerConfig = {
cypressConfig: {
projectRoot,
specPattern: 'src/**/*.cy.ts',
} as Cypress.PluginConfigOptions,
framework: 'angular',
} as WebpackDevServerConfig
const { frameworkConfig: webpackConfig, sourceWebpackModulesResult } = await angularHandler(devServerConfig)
expect(webpackConfig).to.exist
expect((webpackConfig?.entry as any).main).to.be.undefined
expect(sourceWebpackModulesResult.framework?.importPath).to.include(path.join('@angular-devkit', 'build-angular'))
await expectLoadsAngularJson(projectRoot)
await expectLoadsAngularCLiModules(projectRoot)
await expectGeneratesTsConfig(devServerConfig)
expectLoadsAngularBuildOptions()
})
it('sources the config from angular-14', async () => {
const projectRoot = await scaffoldMigrationProject('angular-14')
process.chdir(projectRoot)
const devServerConfig = {
cypressConfig: {
projectRoot,
specPattern: 'src/**/*.cy.ts',
} as Cypress.PluginConfigOptions,
framework: 'angular',
} as WebpackDevServerConfig
const { frameworkConfig: webpackConfig, sourceWebpackModulesResult } = await angularHandler(devServerConfig)
expect(webpackConfig).to.exist
expect((webpackConfig?.entry as any).main).to.be.undefined
expect(sourceWebpackModulesResult.framework?.importPath).to.include(path.join('@angular-devkit', 'build-angular'))
await expectLoadsAngularJson(projectRoot)
await expectLoadsAngularCLiModules(projectRoot)
await expectGeneratesTsConfig(devServerConfig)
expectLoadsAngularBuildOptions()
})
})
const expectLoadsAngularJson = async (projectRoot: string) => {
const angularJson = await getAngularJson(projectRoot)
expect(angularJson).to.not.be.null
await expect(getAngularJson(path.join('..', projectRoot))).to.be.rejected
}
const expectLoadsAngularCLiModules = async (projectRoot: string) => {
const angularCliModules = await getAngularCliModules(projectRoot)
expect(angularCliModules.generateBrowserWebpackConfigFromContext).to.not.be.null
expect(angularCliModules.getStylesConfig).to.not.be.null
expect(angularCliModules.getCommonConfig).to.not.be.null
await expect(getAngularCliModules(path.join('..', projectRoot))).to.be.rejected
}
const expectLoadsAngularBuildOptions = () => {
const tsConfig = 'tsconfig.cypress.json'
let buildOptions = getAngularBuildOptions(projectConfig, tsConfig)
expect(buildOptions.aot).to.be.false
expect(buildOptions.optimization).to.be.false
expect(buildOptions.tsConfig).to.equal(tsConfig)
expect(buildOptions.outputHashing).to.equal('none')
expect(buildOptions.budgets).to.be.undefined
const modifiedProjectConfig = cloneDeep(projectConfig)
delete modifiedProjectConfig.architect.build.configurations
buildOptions = getAngularBuildOptions(modifiedProjectConfig, tsConfig)
expect(buildOptions.optimization).to.be.true
}
const expectGeneratesTsConfig = async (devServerConfig: WebpackDevServerConfig) => {
const { projectRoot } = devServerConfig.cypressConfig
let tsConfigPath = await generateTsConfig(devServerConfig, projectConfig)
const tempDir = await getTempDir()
expect(tsConfigPath).to.eq(path.join(tempDir, 'tsconfig.json'))
let tsConfig = JSON.parse(await fs.readFile(tsConfigPath, 'utf8'))
expect(tsConfig).to.deep.eq({
extends: toPosix(path.join(projectRoot, 'tsconfig.json')),
compilerOptions: {
outDir: toPosix(path.join(projectRoot, 'out-tsc/cy')),
allowSyntheticDefaultImports: true,
skipLibCheck: true,
},
include: [
toPosix(path.join(projectRoot, 'src/**/*.cy.ts')),
toPosix(path.join(projectRoot, 'src/polyfills.ts')),
toPosix(path.join(projectRoot, 'node_modules/cypress/types/index.d.ts')),
],
})
const modifiedProjectConfig = cloneDeep(projectConfig)
delete modifiedProjectConfig.architect.build.options.polyfills
const modifiedDevServerConfig = cloneDeep(devServerConfig)
const supportFile = path.join(projectRoot, 'cypress', 'support', 'component.ts')
modifiedDevServerConfig.cypressConfig.supportFile = supportFile
tsConfigPath = await generateTsConfig(modifiedDevServerConfig, modifiedProjectConfig)
tsConfig = JSON.parse(await fs.readFile(tsConfigPath, 'utf8'))
expect(tsConfig.include).to.deep.equal([
toPosix(path.join(projectRoot, 'src/**/*.cy.ts')),
toPosix(supportFile),
toPosix(path.join(projectRoot, 'node_modules/cypress/types/index.d.ts')),
])
}