Skip to content

Commit 325928e

Browse files
authored
feat: add jest-support (#89)
1 parent 24054b2 commit 325928e

27 files changed

+1769
-343
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
node-version: 18
1616
- uses: pnpm/action-setup@v2
1717
with:
18-
version: 7.27.1
18+
version: 8.8.0
1919
- name: Install Packages
2020
run: |
2121
pnpm i
@@ -31,7 +31,7 @@ jobs:
3131
cd examples
3232
npx playwright install --with-deps
3333
cd apps/ng-app-cli
34-
npx ng add ../../../packages/angular --skip-confirmation
34+
# npx ng add ../../../packages/angular --skip-confirmation
3535
npx tsc --noEmit --project tsconfig.app.json
3636
cd -
3737
pnpm build:all

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
npm test

examples/apps/ng-app-cli/.env

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
USER_HOME=/home/chihab
21
NGX_VERSION=IGNORED_SINCE_SET_IN_ENV_LOCAL
3-
NGX_USER_HOME=$USER_HOME
2+
NGX_BRANCH=main
43

examples/apps/ng-app-cli/.env.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
NGX_VERSION=12.0.0
2+
NGX_BRANCH=test

examples/apps/ng-app-cli/angular.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
},
3737
"assets": ["src/favicon.ico", "src/assets"],
3838
"styles": ["src/styles.css"],
39-
"scripts": [],
4039
"server": "src/main.server.ts",
4140
"prerender": true,
4241
"ssr": {
@@ -190,7 +189,8 @@
190189
"tsConfig": "tsconfig.spec.json",
191190
"karmaConfig": "karma.conf.js",
192191
"ngxEnv": {
193-
"prefix": "NGX_"
192+
"prefix": "NGX_",
193+
"unsecure": true
194194
},
195195
"assets": ["src/favicon.ico", "src/assets"],
196196
"styles": ["src/styles.css"],
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const jestConfig = {
2+
preset: "jest-preset-angular/presets/defaults-esm",
3+
setupFilesAfterEnv: ["<rootDir>/setup-jest.ts"],
4+
testMatch: ["<rootDir>/src/app/**/*.(spec|jest).ts"],
5+
cache: false,
6+
moduleNameMapper: {
7+
"^rxjs(/operators$)?$":
8+
"<rootDir>/node_modules/rxjs/dist/bundles/rxjs.umd.js",
9+
tslib: "<rootDir>/node_modules/tslib/tslib.es6.mjs",
10+
},
11+
transform: {
12+
"^.+\\.(ts)$": [
13+
"@dotenv-run/jest-angular",
14+
{
15+
useESM: true,
16+
},
17+
],
18+
},
19+
};
20+
21+
export default jestConfig;

examples/apps/ng-app-cli/package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng-app-cli",
3-
"version": "0.0.0",
3+
"version": "10",
44
"scripts": {
55
"ng": "ng",
66
"watch": "ng build --watch --configuration development",
@@ -11,7 +11,8 @@
1111
"build:esbuild-browser": "NODE_ENV=production ng run ng-app-cli:build-be",
1212
"build:browser": "NODE_ENV=production ng run ng-app-cli:build-browser",
1313
"pretest": "npx ng config projects.ng-app-cli.architect.test.options.ngxEnv.prefix 'NGX_'",
14-
"test": "NODE_ENV=test NGX_BRANCH_NAME=main ng test --watch=false",
14+
"test": "NODE_ENV=test NGX_BRANCH=test ng test --watch=false",
15+
"test:jest": "NODE_OPTIONS=--experimental-vm-modules NODE_ENV=test npx jest",
1516
"e2e": "playwright test",
1617
"static": "serve dist/ng-app-cli/browser/fr -p 8081"
1718
},
@@ -47,6 +48,13 @@
4748
"karma-coverage": "~2.2.0",
4849
"karma-jasmine": "~5.1.0",
4950
"karma-jasmine-html-reporter": "~2.1.0",
50-
"typescript": "~5.2.2"
51+
"typescript": "~5.2.2",
52+
"ts-jest": "^29.1.2",
53+
"@jest/transform": "^29.7.0",
54+
"esbuild": "0.20.0",
55+
"jest": "^29.7.0",
56+
"jest-preset-angular": "14.0.0",
57+
"@dotenv-run/core": "*",
58+
"@dotenv-run/jest-angular": "*"
5159
}
5260
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { env } from '@dotenv-run/core';
2+
env({ root: '../../..', files: ['.env', '.env.app'] });
3+
import 'jest-preset-angular/setup-jest.mjs';
4+
5+
import '@angular/localize/init';
6+
import { TextEncoder } from 'util';
7+
Object.defineProperty(window, 'TextEncoder', {
8+
writable: true,
9+
value: TextEncoder,
10+
});

examples/apps/ng-app-cli/src/app/app.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ <h2>{{ title }}</h2>
2121
</td>
2222
</tr>
2323
<tr>
24-
<td>USER_HOME</td>
24+
<td>BRANCH</td>
2525
<td>
26-
<span id="USER_HOME">{{ home }}</span>
26+
<span id="VERSION"> {{ branch }} </span>
2727
</td>
2828
</tr>
2929
</tbody>
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
import { TestBed } from '@angular/core/testing';
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
22
import { AppComponent } from './app.component';
33

44
describe('AppComponent', () => {
5+
let component: AppComponent;
6+
let fixture: ComponentFixture<AppComponent>;
7+
58
beforeEach(async () => {
69
await TestBed.configureTestingModule({
710
imports: [AppComponent],
811
}).compileComponents();
12+
fixture = TestBed.createComponent(AppComponent);
13+
component = fixture.componentInstance;
14+
fixture.detectChanges();
915
});
1016

11-
it('should create the app', () => {
12-
const fixture = TestBed.createComponent(AppComponent);
13-
const app = fixture.componentInstance;
14-
expect(app).toBeTruthy();
17+
it('should create the app 2', () => {
18+
expect(component).toBeTruthy();
1519
});
1620

1721
it(`should have the 'Hello world' title`, () => {
18-
const fixture = TestBed.createComponent(AppComponent);
19-
const app = fixture.componentInstance;
20-
expect(app.title).toEqual('Hello world');
22+
expect(component.title).toEqual('Hello world');
23+
expect(component.branch).toEqual('test');
2124
});
2225
});

0 commit comments

Comments
 (0)