node.js - Jest TypeScript: utvidet type finnes ikke på typen T
Jeg tester opprettelsen av en ny billett-enhet med Jest i mitt Node/TS-prosjekt. Typene for en billett er gitt nedenfor:
export interface Entity {
getId: () => string;
getTitle: () => string;
}
export interface Ticket extends Entity {
getDetails: () => string;
getCategory: () => CategoryOptions;
getStatus: () => StatusOptions;
getCreatedAt: () => Date;
getUpdatedAt: () => Date;
}
Som du kan se, utvider Ticket egenskapene til en generell Entity-type. Det er ingen typefeil i min faktiske kode, men inne i testsettet mitt prøver jeg:
it("should create new ticket entity", () => {
const ticket: Ticket = makeTicket(ticketInfo);
expect(ticket.getId()).toBe("1234567890");
});
som gir følgende typefeil
Egenskapen 'getId' eksisterer ikke på typen 'Ticket'. ts(2339)
Her er min babel.config.ts-fil:
export default {
// Stop running tests after `n` failures
bail: 3,
// Automatically clear mock calls and instances between every test
clearMocks: true,
// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,
// The directory where Jest should output its coverage files
coverageDirectory: "coverage",
// An array of regexp pattern strings used to skip coverage collection
coveragePathIgnorePatterns: [
"/node_modules/"
],
// Indicates which provider should be used to instrument code for coverage
coverageProvider: "v8",
// A list of reporter names that Jest uses when writing coverage reports
coverageReporters: [
"json",
"html",
"text"
],
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: -10
}
},
// An array of file extensions your modules use
moduleFileExtensions: [
"js",
"ts"
],
// A preset that is used as a base for Jest's configuration
preset: "ts-jest",
// The root directory that Jest should scan for tests and modules within
rootDir: ".",
// A list of paths to directories that Jest should use to search for files in
roots: [
"<rootDir>/src"
],
// The test environment that will be used for testing
testEnvironment: "node",
// The glob patterns Jest uses to detect test files
testMatch: [
"**/__tests__/**/*.[jt]s?(x)",
"**/?(*.)+(spec|test).[tj]s?(x)"
],
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
testPathIgnorePatterns: [
"/node_modules/",
"/api/",
"/config/"
],
};
Og mine relevante package.json-avhengigheter:
"devDependencies": {
"@babel/cli": "^7.13.10",
"@babel/core": "^7.13.14",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/preset-env": "^7.13.12",
"@babel/preset-typescript": "^7.13.0",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.25",
"@typescript-eslint/eslint-plugin": "^4.20.0",
"@typescript-eslint/parser": "^4.20.0",
"babel-plugin-module-resolver": "^4.1.0",
"eslint": "^7.19.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.3.6",
"jest": "^26.6.3",
"ts-jest": "^26.5.6",
"ts-node": "^9.1.1",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.1.4",
}
Noen ideer om hvordan man kan sikre at Jest kan lese typene til overordnet typer?