Skip to content

Commit 2f598e8

Browse files
committed
Add Jest config and e2e auth tests to API package
Introduces a Jest configuration for the API package to enable TypeScript ESM testing. Adds initial end-to-end tests for authentication and organization/team creation. Updates the test script in package.json and improves static file serving path resolution in app.module.ts.
1 parent 88873a0 commit 2f598e8

File tree

4 files changed

+116
-3
lines changed

4 files changed

+116
-3
lines changed

packages/api/jest.config.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = {
2+
preset: 'ts-jest/presets/default-esm',
3+
testEnvironment: 'node',
4+
testMatch: ['**/test/**/*.test.ts'],
5+
transform: {
6+
'^.+\\.(t|j|m)s$': ['ts-jest', {
7+
useESM: true,
8+
tsconfig: {
9+
module: 'esnext',
10+
moduleResolution: 'node',
11+
allowJs: true,
12+
allowSyntheticDefaultImports: true,
13+
esModuleInterop: true
14+
}
15+
}]
16+
},
17+
extensionsToTreatAsEsm: ['.ts'],
18+
moduleFileExtensions: ['js', 'json', 'ts', 'mjs'],
19+
rootDir: '.',
20+
moduleNameMapper: {
21+
'^(\\.{1,2}/.*)\\.js$': '$1',
22+
},
23+
transformIgnorePatterns: [
24+
'node_modules/(?!better-auth)'
25+
],
26+
};

packages/api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"start": "nest start",
99
"start:debug": "nest start --debug --watch",
1010
"start:prod": "node dist/main",
11-
"test": "echo \"No tests specified\" && exit 0",
11+
"test": "NODE_OPTIONS='--experimental-vm-modules' jest",
1212
"build:client": "npm run build --workspace=@objectql/client"
1313
},
1414
"dependencies": {

packages/api/src/app.module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ import { AppService } from './app.service';
44
import { ObjectQLModule } from './objectql/objectql.module';
55
import { AuthModule } from './auth/auth.module';
66
import { ServeStaticModule } from '@nestjs/serve-static';
7-
import { join } from 'path';
7+
import { join, resolve } from 'path';
88
import { DataController } from './controllers/data.controller';
99
import { MetadataController } from './controllers/metadata.controller';
1010
import { AuthMiddleware } from './auth/auth.middleware';
1111

12+
const clientDistPath = resolve(process.cwd(), process.cwd().endsWith('api') ? '../client/dist' : 'packages/client/dist');
13+
1214
@Module({
1315
imports: [
1416
ObjectQLModule,
1517
AuthModule,
1618
ServeStaticModule.forRoot({
17-
rootPath: join(__dirname, '../../client/dist'),
19+
rootPath: clientDistPath,
1820
exclude: ['/api/(.*)', '/docs/(.*)'],
1921
}),
2022
],

packages/api/test/auth.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { INestApplication } from '@nestjs/common';
3+
import * as request from 'supertest';
4+
import { AppModule } from '../src/app.module';
5+
6+
describe('AuthController (e2e)', () => {
7+
let app: INestApplication;
8+
let agent: any;
9+
10+
beforeAll(async () => {
11+
const moduleFixture: TestingModule = await Test.createTestingModule({
12+
imports: [AppModule],
13+
}).compile();
14+
15+
app = moduleFixture.createNestApplication();
16+
await app.init();
17+
agent = request.agent(app.getHttpServer());
18+
});
19+
20+
afterAll(async () => {
21+
await app.close();
22+
});
23+
24+
const uniqueId = Date.now();
25+
const email = `test.user.${uniqueId}@example.com`;
26+
const password = 'Password123!';
27+
const orgName = `Test Org ${uniqueId}`;
28+
let organizationId: string;
29+
30+
it('should sign up a new user', async () => {
31+
const response = await agent
32+
.post('/api/v6/auth/sign-up/email')
33+
.send({
34+
email,
35+
password,
36+
name: 'Test User',
37+
})
38+
.expect(200);
39+
40+
expect(response.body).toHaveProperty('user');
41+
expect(response.body.user.email).toBe(email);
42+
expect(response.body.user).toHaveProperty('role');
43+
});
44+
45+
it('should sign in', async () => {
46+
// In case sign up doesn't auto sign in (better-auth usually does), but testing sign-in is good.
47+
await agent
48+
.post('/api/v6/auth/sign-in/email')
49+
.send({
50+
email,
51+
password
52+
})
53+
.expect(200);
54+
});
55+
56+
it('should create an organization', async () => {
57+
const response = await agent
58+
.post('/api/v6/auth/organization/create')
59+
.send({
60+
name: orgName,
61+
slug: `test-org-${uniqueId}`
62+
})
63+
.expect(200);
64+
65+
expect(response.body).toHaveProperty('id');
66+
expect(response.body.name).toBe(orgName);
67+
organizationId = response.body.id;
68+
});
69+
70+
it('should create a team (verifying team and teamMember tables)', async () => {
71+
expect(organizationId).toBeDefined();
72+
73+
const response = await agent
74+
.post('/api/v6/auth/organization/create-team')
75+
.send({
76+
name: 'Dev Team',
77+
organizationId: organizationId,
78+
slug: `dev-team-${uniqueId}`
79+
})
80+
.expect(200);
81+
82+
expect(response.body).toHaveProperty('id');
83+
expect(response.body.name).toBe('Dev Team');
84+
});
85+
});

0 commit comments

Comments
 (0)