Вот мои тесты:
Для setTimeout
, jasmine.clock()
и jasmine.clock().tick(...)
отлично работает.
index.ts
:
import * as mod from './mod';
export function main() {
setTimeout(() => {
mod.foo();
}, 10);
}
mod.ts
:
export function foo() {}
index.spec.ts
:
import { main } from './';
import * as mod from './mod';
fdescribe('setImmediate', () => {
beforeEach(function() {
jasmine.clock().install();
});
afterEach(function() {
jasmine.clock().uninstall();
});
it('should pass', () => {
const fooSpy: jasmine.Spy = spyOn(mod, 'foo').and.stub();
main();
jasmine.clock().tick(10);
expect(fooSpy).toHaveBeenCalledTimes(1);
});
});
Результаты юнит-теста:
HeadlessChrome 73.0.3683 (Mac OS X 10.13.6): Executed 1 of 4 (skipped 3) SUCCESS (0.029 secs / 0.004 secs)
TOTAL: 1 SUCCESS
TOTAL: 1 SUCCESS
TOTAL: 1 SUCCESS
Если заменить setTimeout
на setImmediate
, юнит-тест не удается.
index.ts
:
import * as mod from './mod';
export function main() {
setImmediate(() => {
mod.foo();
});
}
Результаты модульного теста:
HeadlessChrome 73.0.3683 (Mac OS X 10.13.6) setImmediate should pass FAILED
Error: Expected spy foo to have been called once. It was called 0 times.
at <Jasmine>
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/tests/setImmediate/index.spec.ts:16:20)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone.js:391:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308:1)
HeadlessChrome 73.0.3683 (Mac OS X 10.13.6): Executed 1 of 4 (1 FAILED) (0 secs / 0.104 secs)
HeadlessChrome 73.0.3683 (Mac OS X 10.13.6) setImmediate should pass FAILED
Error: Expected spy foo to have been called once. It was called 0 times.
at <Jasmine>
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/tests/setImmediate/index.spec.ts:16:20)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone.js:391:1)
HeadlessChrome 73.0.3683 (Mac OS X 10.13.6): Executed 1 of 4 (1 FAILED) (skipped 3) (0.136 secs / 0.104 secs)
TOTAL: 1 FAILED, 0 SUCCESS
TOTAL: 1 FAILED, 0 SUCCESS
TOTAL: 1 FAILED, 0 SUCCESS
Обнаружена связанная проблема: https://github.com/jasmine/jasmine/issues/866, но проблема немного устарела, и на нее нет ответа.
Моя тестовая среда angular 8.x с использованием команды ng test
. Я не думаю, что эта проблема связана с тестовой средой. И версии узловых модулей:
"jasmine-core": "^3.5.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "^4.4.1",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage-istanbul-reporter": "^2.1.1",
"karma-jasmine": "^3.1.0",
"karma-jasmine-html-reporter": "^1.5.1",