Skip to content

Commit 70713bf

Browse files
devoto13filipesilva
authored andcommitted
feat(@angular/cli): add flag to specify environment for ng test command
1 parent 8bad46e commit 70713bf

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

docs/documentation/test.md

+10
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ You can run tests with coverage via `--code-coverage`. The coverage report will
6666
</p>
6767
</details>
6868

69+
<details>
70+
<summary>environment</summary>
71+
<p>
72+
<code>--environment</code> (aliases: <code>-e</code>)
73+
</p>
74+
<p>
75+
Defines the build environment.
76+
</p>
77+
</details>
78+
6979
<details>
7080
<summary>log-level</summary>
7181
<p>

packages/@angular/cli/commands/test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface TestOptions {
1919
progress?: boolean;
2020
config: string;
2121
poll?: number;
22+
environment?: string;
2223
app?: string;
2324
}
2425

@@ -100,6 +101,12 @@ const TestCommand = Command.extend({
100101
default: pollDefault,
101102
description: 'Enable and define the file watching poll time period (milliseconds).'
102103
},
104+
{
105+
name: 'environment',
106+
type: String,
107+
aliases: ['e'] ,
108+
description: 'Defines the build environment.'
109+
},
103110
{
104111
name: 'app',
105112
type: String,

packages/@angular/cli/tasks/test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default Task.extend({
3434
sourcemaps: options.sourcemaps,
3535
progress: options.progress,
3636
poll: options.poll,
37+
environment: options.environment,
3738
app: options.app
3839
};
3940

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ng } from '../../utils/process';
2+
import { writeFile } from '../../utils/fs';
3+
4+
export default function () {
5+
// Tests run in 'dev' environment by default.
6+
return writeFile('src/app/environment.spec.ts', `
7+
import { environment } from '../environments/environment';
8+
9+
describe('Test environment', () => {
10+
it('should have production disabled', () => {
11+
expect(environment.production).toBe(false);
12+
});
13+
});
14+
`)
15+
.then(() => ng('test', '--single-run'))
16+
17+
// Tests can run in different environment.
18+
.then(() => writeFile('src/app/environment.spec.ts', `
19+
import { environment } from '../environments/environment';
20+
21+
describe('Test environment', () => {
22+
it('should have production enabled', () => {
23+
expect(environment.production).toBe(true);
24+
});
25+
});
26+
`))
27+
.then(() => ng('test', '-e', 'prod', '--single-run'));
28+
}

0 commit comments

Comments
 (0)