I am trying to run tests on a web application I built with the C8Y CLI but I am running into a couple of problems.
I have organized my source code into two folders: app
and test
. The former stores all of my application code and the latter is reserved for executing unit tests on it.
src
├── app
├── live-dashboard
├── live-dashboard.component.html
├── live-dashboard.component.less
├── live-dashboard.component.ts
├── live-dashboard.factory.ts
├── live-dashboard.module.ts
├── live-dashboard.service.ts
├── test
├── live-dashboard.component.spec.ts
Here is my test file:
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from '@angular/core';
import { LiveDashboardComponent} from 'src/app/live-dashboard/live-dashboard.component'
import { LiveDashboardService } from 'src/app/live-dashboard/live-dashboard.service';
describe('LiveDashboardComponent test', () => {
let fixture: ComponentFixture<LiveDashboardComponent>;
let component: LiveDashboardComponent;
let service: LiveDashboardService
// Init Angular Test Environment
beforeAll(() => {
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamic())
});
// Configure Testing Module
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
LiveDashboardComponent
],
providers: [
LiveDashboardService
],
schemas: [
CUSTOM_ELEMENTS_SCHEMA,
NO_ERRORS_SCHEMA
]
}).compileComponents(); // necessary for when our components use templateUrl
// and styleUrls to load external files (css and html)
// that compiler has to read asynchronously.
})
// Configure component and service(s)
beforeEach(() => {
fixture = TestBed.createComponent(LiveDashboardComponent);
component = fixture.componentInstance;
fixture.detectChanges(); // component will start with ngOnInit
service = fixture.debugElement.injector.get(LiveDashboardService);
})
test('Component should be defined', () => {
expect(component).toBeDefined();
})
test.skip('Test the getModeFragmentSeries', () => {
})
afterEach(() => {
fixture.destroy(); // Destroys the fixture and its contents
component = null; // Resets the component reference
fixture = null; // Resets the fixture reference
});
});
When I try to run npm test
, however, I keep getting an error stating that the test file cannot find the
FAIL src/app/live-dashboard/live-dashboard.component.spec.ts
● Test suite failed to run
Cannot find module 'src/app/live-dashboard/live-dashboard.component' from 'src/app/live-dashboard/live-dashboard.component.spec.ts'
4 | import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from '@angular/core';
5 |
> 6 | import { LiveDashboardComponent} from 'src/app/live-dashboard/live-dashboard.component'
| ^
7 | import { LiveDashboardService } from 'src/app/live-dashboard/live-dashboard.service';
8 |
9 | describe('LiveDashboardComponent test', () => {
at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:491:11)
at Object.<anonymous> (src/app/live-dashboard/live-dashboard.component.spec.ts:6:1)
Test Suites: 2 failed, 2 total
Tests: 0 total
Snapshots: 0 total
Time: 5.933 s
Ran all test suites.
I have tried moving the test file to the app
folder but I just got the same response.
In addition, I am getting another error, this time with my first test
block stating that the property 'toBeDefined' does not exist on type 'Assertion'.ts(2339)
.
I hovered over the expect
method and discovered that it is using the expect method from the Chai framework:
var expect: Chai.ExpectStatic (val: any, message?: string) => Chai.Assertion
How can this be if the project is already set up to use Jest as its testing framework? How would I get it to use Jest’s expect
method instead?