JEST it.each([]) 在 NestJS 测试期间不扩展 getter
JEST it.each([]) not expanding getter during test in NestJS
我已将其简化为独立测试,我相信我只是错误地安排了测试。但是,当我可以直接测试服务而不是 it.each().
时,我无法弄清楚为什么没有正确创建服务
我正在尝试使用 it.each() 测试服务的 getter 数据,但它不想在 Nest JS 结构中工作。
我已经为我的问题创建了一个非常简单的示例。我的服务将被共享,所以它在一个 LibraryModule 中,然后在需要的地方导入。但是,我的问题只是测试服务本身。
图书馆模块
import { Module } from '@nestjs/common';
import { MyService } from './my.service';
@Module({
providers: [MyService],
exports: [MyService],
})
export class LibraryModule {}
MyService(非常基本,示例简单 getter)
import { Injectable } from '@nestjs/common';
@Injectable()
export class MyService {
public get Description(): string {
return 'text';
}
}
然后我使用为最初由 NestJS 原理图生成的服务生成的规范文件。
import { Test, TestingModule } from '@nestjs/testing';
import { LibraryModule } from '@LIBRARY/library.module';
import { MyService } from './my.service';
describe('test with new() - This works.', () => {
const TestClass: MyService = new MyService();
it('should be defined', () => {
expect(TestClass).toBeDefined();
});
it('should return the Description', () => {
expect(TestClass.Description).not.toBeNull();
expect(TestClass.Description).not.toBeUndefined();
expect(TestClass.Description).toBe('text');
});
it.each([['Description', TestClass.Description, 'text']])(
'should return the text for %s',
(TestDescription: string, Actual: string, Expected: string) => {
expect(Actual).not.toBeNull();
expect(Actual).not.toBeUndefined();
expect(Actual).toBe(Expected);
}
);
});
以上测试有效,包括 it.each()。但是,我有第二组测试使用 NestJS 测试框架来创建服务。
describe('test with MyService', () => {
let service: MyService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [LibraryModule],
providers: [MyService],
exports: [MyService],
}).compile();
service = module.get<MyService>(MyService);
});
it('should be defined - Works', () => {
expect(service).toBeDefined();
});
it('should return the Description - Works', () => {
expect(service.Description).not.toBeNull();
expect(service.Description).not.toBeUndefined();
expect(service.Description).toBe('text');
});
it.each([['Description', service.Description, 'text']])(
'should return the text for %s - FAILS',
(TestDescription: string, Actual: string, Expected: string) => {
expect(Actual).not.toBeNull();
expect(Actual).not.toBeUndefined();
expect(Actual).toBe(Expected);
}
);
});
直接使用 service.Description 可以,但是在 it.each() 中使用 service.Description 会出错:
TypeError: Cannot read property 'Description' of undefined
54 |
> 55 | it.each([['Description', service.Description, 'text']])(
| ^
56 | 'should return the text for %s',
57 | (TestDescription: string, Actual: string, Expected: string) => {
58 | expect(Actual).not.toBeNull();
我不明白为什么当我创建服务时它与 it.each() 一起工作,但是当我允许 NestJS 测试创建它时,it.each() 失败,但是直接使用它进行测试 (service.Description) 有效。这意味着服务已创建,但我不知道为什么它不能与 it.each()?
一起使用
看起来这是 Jest 的局限性,与 Nest 无关。如果你使用下面的测试
import { Test } from '@nestjs/testing';
import { AppService } from './app.service';
describe('AppService', () => {
let service: AppService;
let dynamicValue: string;
beforeEach(async () => {
const modRef = await Test.createTestingModule({
providers: [AppService],
}).compile();
service = modRef.get(AppService);
dynamicValue = Math.random().toString();
});
it.each(['string', dynamicValue, 'another string'])('%s', () => {
expect(service.getHello()).toBe('Hello World!');
});
});
你会得到输出
PASS src/app.service.spec.ts
AppService
✓ string (48 ms)
✓ undefined (10 ms)
✓ another string (10 ms)
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 3.92 s
Ran all test suites matching /app.service/i
这表明 beforeEach
中确定的值在 it.each
中不可用。 beforeAll
也是如此。它适用于您的第一个测试,因为您在定义任何测试或对测试的引用之前创建了测试 class,它本质上是测试套件的定义变量,而不是已声明的变量。
我已将其简化为独立测试,我相信我只是错误地安排了测试。但是,当我可以直接测试服务而不是 it.each().
时,我无法弄清楚为什么没有正确创建服务我正在尝试使用 it.each() 测试服务的 getter 数据,但它不想在 Nest JS 结构中工作。
我已经为我的问题创建了一个非常简单的示例。我的服务将被共享,所以它在一个 LibraryModule 中,然后在需要的地方导入。但是,我的问题只是测试服务本身。
图书馆模块
import { Module } from '@nestjs/common';
import { MyService } from './my.service';
@Module({
providers: [MyService],
exports: [MyService],
})
export class LibraryModule {}
MyService(非常基本,示例简单 getter)
import { Injectable } from '@nestjs/common';
@Injectable()
export class MyService {
public get Description(): string {
return 'text';
}
}
然后我使用为最初由 NestJS 原理图生成的服务生成的规范文件。
import { Test, TestingModule } from '@nestjs/testing';
import { LibraryModule } from '@LIBRARY/library.module';
import { MyService } from './my.service';
describe('test with new() - This works.', () => {
const TestClass: MyService = new MyService();
it('should be defined', () => {
expect(TestClass).toBeDefined();
});
it('should return the Description', () => {
expect(TestClass.Description).not.toBeNull();
expect(TestClass.Description).not.toBeUndefined();
expect(TestClass.Description).toBe('text');
});
it.each([['Description', TestClass.Description, 'text']])(
'should return the text for %s',
(TestDescription: string, Actual: string, Expected: string) => {
expect(Actual).not.toBeNull();
expect(Actual).not.toBeUndefined();
expect(Actual).toBe(Expected);
}
);
});
以上测试有效,包括 it.each()。但是,我有第二组测试使用 NestJS 测试框架来创建服务。
describe('test with MyService', () => {
let service: MyService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [LibraryModule],
providers: [MyService],
exports: [MyService],
}).compile();
service = module.get<MyService>(MyService);
});
it('should be defined - Works', () => {
expect(service).toBeDefined();
});
it('should return the Description - Works', () => {
expect(service.Description).not.toBeNull();
expect(service.Description).not.toBeUndefined();
expect(service.Description).toBe('text');
});
it.each([['Description', service.Description, 'text']])(
'should return the text for %s - FAILS',
(TestDescription: string, Actual: string, Expected: string) => {
expect(Actual).not.toBeNull();
expect(Actual).not.toBeUndefined();
expect(Actual).toBe(Expected);
}
);
});
直接使用 service.Description 可以,但是在 it.each() 中使用 service.Description 会出错:
TypeError: Cannot read property 'Description' of undefined
54 |
> 55 | it.each([['Description', service.Description, 'text']])(
| ^
56 | 'should return the text for %s',
57 | (TestDescription: string, Actual: string, Expected: string) => {
58 | expect(Actual).not.toBeNull();
我不明白为什么当我创建服务时它与 it.each() 一起工作,但是当我允许 NestJS 测试创建它时,it.each() 失败,但是直接使用它进行测试 (service.Description) 有效。这意味着服务已创建,但我不知道为什么它不能与 it.each()?
一起使用看起来这是 Jest 的局限性,与 Nest 无关。如果你使用下面的测试
import { Test } from '@nestjs/testing';
import { AppService } from './app.service';
describe('AppService', () => {
let service: AppService;
let dynamicValue: string;
beforeEach(async () => {
const modRef = await Test.createTestingModule({
providers: [AppService],
}).compile();
service = modRef.get(AppService);
dynamicValue = Math.random().toString();
});
it.each(['string', dynamicValue, 'another string'])('%s', () => {
expect(service.getHello()).toBe('Hello World!');
});
});
你会得到输出
PASS src/app.service.spec.ts
AppService
✓ string (48 ms)
✓ undefined (10 ms)
✓ another string (10 ms)
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 3.92 s
Ran all test suites matching /app.service/i
这表明 beforeEach
中确定的值在 it.each
中不可用。 beforeAll
也是如此。它适用于您的第一个测试,因为您在定义任何测试或对测试的引用之前创建了测试 class,它本质上是测试套件的定义变量,而不是已声明的变量。