UNPKG

@definitelytyped/dts-critic

Version:

Checks a new .d.ts against the Javascript source and tells you what problems it has

146 lines (143 loc) 7.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /// <reference types="jest" /> const index_1 = require("./index"); function suite(description, tests) { describe(description, () => { for (const k in tests) { test(k, tests[k], 10 * 1000); } }); } suite("findDtsName", { absolutePath() { expect((0, index_1.findDtsName)("~/dt/types/jquery/index.d.ts")).toBe("jquery"); }, relativePath() { expect((0, index_1.findDtsName)("jquery/index.d.ts")).toBe("jquery"); }, currentDirectory() { expect((0, index_1.findDtsName)("index.d.ts")).toBe("DefinitelyTyped-tools"); }, relativeCurrentDirectory() { expect((0, index_1.findDtsName)("./index.d.ts")).toBe("DefinitelyTyped-tools"); }, emptyDirectory() { expect((0, index_1.findDtsName)("")).toBe("DefinitelyTyped-tools"); }, }); suite("dtToNpmName", { nonScoped() { expect((0, index_1.dtToNpmName)("content-type")).toBe("content-type"); }, scoped() { expect((0, index_1.dtToNpmName)("babel__core")).toBe("@babel/core"); }, }); suite("parseExportErrorKind", { existent() { expect((0, index_1.parseExportErrorKind)("NoDefaultExport")).toBe(index_1.ErrorKind.NoDefaultExport); }, existentDifferentCase() { expect((0, index_1.parseExportErrorKind)("JspropertyNotinDTS")).toBe(index_1.ErrorKind.JsPropertyNotInDts); }, nonexistent() { expect((0, index_1.parseExportErrorKind)("FakeError")).toBe(undefined); }, }); const allErrors = new Map([ [index_1.ErrorKind.NeedsExportEquals, true], [index_1.ErrorKind.NoDefaultExport, true], [index_1.ErrorKind.JsSignatureNotInDts, true], [index_1.ErrorKind.DtsSignatureNotInJs, true], [index_1.ErrorKind.DtsPropertyNotInJs, true], [index_1.ErrorKind.JsPropertyNotInDts, true], ]); function testsource(filename) { return __dirname + "/testsource/" + filename; } suite("checkSource", { noErrors() { expect((0, index_1.checkSource)("noErrors", testsource("noErrors.d.ts"), testsource("noErrors.js"), allErrors, false)).toEqual([]); }, missingJsProperty() { expect((0, index_1.checkSource)("missingJsProperty", testsource("missingJsProperty.d.ts"), testsource("missingJsProperty.js"), allErrors, false)).toEqual(expect.arrayContaining([ { kind: index_1.ErrorKind.JsPropertyNotInDts, message: `The declaration doesn't match the JavaScript module 'missingJsProperty'. Reason: The JavaScript module exports a property named 'foo', which is missing from the declaration module.`, }, ])); }, noMissingWebpackProperty() { expect((0, index_1.checkSource)("missingJsProperty", testsource("webpackPropertyNames.d.ts"), testsource("webpackPropertyNames.js"), allErrors, false)).toHaveLength(0); }, missingDtsProperty() { expect((0, index_1.checkSource)("missingDtsProperty", testsource("missingDtsProperty.d.ts"), testsource("missingDtsProperty.js"), allErrors, false)).toEqual(expect.arrayContaining([ { kind: index_1.ErrorKind.DtsPropertyNotInJs, message: `The declaration doesn't match the JavaScript module 'missingDtsProperty'. Reason: The declaration module exports a property named 'foo', which is missing from the JavaScript module.`, position: { start: 65, length: 11, }, }, ])); }, missingDefaultExport() { expect((0, index_1.checkSource)("missingDefault", testsource("missingDefault.d.ts"), testsource("missingDefault.js"), allErrors, false)).toEqual(expect.arrayContaining([ { kind: index_1.ErrorKind.NoDefaultExport, message: `The declaration doesn't match the JavaScript module 'missingDefault'. Reason: The declaration specifies 'export default' but the JavaScript source does not mention 'default' anywhere. The most common way to resolve this error is to use 'export =' syntax instead of 'export default'. To learn more about 'export =' syntax, see https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require.`, position: { start: 0, length: 33, }, }, ])); }, missingJsSignatureExportEquals() { expect((0, index_1.checkSource)("missingJsSignatureExportEquals", testsource("missingJsSignatureExportEquals.d.ts"), testsource("missingJsSignatureExportEquals.js"), allErrors, false)).toEqual(expect.arrayContaining([ { kind: index_1.ErrorKind.JsSignatureNotInDts, message: `The declaration doesn't match the JavaScript module 'missingJsSignatureExportEquals'. Reason: The JavaScript module can be called or constructed, but the declaration module cannot.`, }, ])); }, missingJsSignatureNoExportEquals() { expect((0, index_1.checkSource)("missingJsSignatureNoExportEquals", testsource("missingJsSignatureNoExportEquals.d.ts"), testsource("missingJsSignatureNoExportEquals.js"), allErrors, false)).toEqual(expect.arrayContaining([ { kind: index_1.ErrorKind.JsSignatureNotInDts, message: `The declaration doesn't match the JavaScript module 'missingJsSignatureNoExportEquals'. Reason: The JavaScript module can be called or constructed, but the declaration module cannot. The most common way to resolve this error is to use 'export =' syntax. To learn more about 'export =' syntax, see https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require.`, }, ])); }, missingDtsSignature() { expect((0, index_1.checkSource)("missingDtsSignature", testsource("missingDtsSignature.d.ts"), testsource("missingDtsSignature.js"), allErrors, false)).toEqual(expect.arrayContaining([ { kind: index_1.ErrorKind.DtsSignatureNotInJs, message: `The declaration doesn't match the JavaScript module 'missingDtsSignature'. Reason: The declaration module can be called or constructed, but the JavaScript module cannot.`, }, ])); }, missingExportEquals() { expect((0, index_1.checkSource)("missingExportEquals", testsource("missingExportEquals.d.ts"), testsource("missingExportEquals.js"), allErrors, false)).toEqual(expect.arrayContaining([ { kind: index_1.ErrorKind.NeedsExportEquals, message: `The declaration doesn't match the JavaScript module 'missingExportEquals'. Reason: The declaration should use 'export =' syntax because the JavaScript source uses 'module.exports =' syntax and 'module.exports' can be called or constructed. To learn more about 'export =' syntax, see https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require.`, }, ])); }, }); //# sourceMappingURL=index.test.js.map