UNPKG

@microsoft/api-extractor

Version:

Validatation, documentation, and auditing for the exported API of a TypeScript package

70 lines (68 loc) 3.79 kB
/// <reference types="mocha" /> "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var chai_1 = require("chai"); var ts = require("typescript"); var path = require("path"); var Extractor_1 = require("../../Extractor"); var ApiDocumentation_1 = require("../ApiDocumentation"); /* tslint:disable:no-function-expression - Mocha uses a poorly scoped "this" pointer */ var capturedErrors = []; function testErrorHandler(message, fileName, lineNumber) { capturedErrors.push({ message: message, fileName: fileName, lineNumber: lineNumber }); } var inputFolder = './testInputs/example2'; var compilerOptions = { target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS, moduleResolution: ts.ModuleResolutionKind.NodeJs, rootDir: inputFolder, typeRoots: ['./'] // We need to ignore @types in these tests }; var extractor = new Extractor_1.default({ compilerOptions: compilerOptions, errorHandler: testErrorHandler }); extractor.loadExternalPackages('./testInputs/external-api-json'); // Run the analyzer once to be used by unit tests extractor.analyze({ entryPointFile: path.join(inputFolder, 'src/index.ts') }); // These warnings would normally be printed at the bottom // of the source package's '*.api.ts' file. var warnings = []; var myDocumentedClass = extractor.package.getSortedMemberItems() .filter(function (apiItem) { return apiItem.name === 'MyDocumentedClass'; })[0]; describe('ApiDocumentation tests', function () { this.timeout(10000); describe('ApiDocumentation internal methods', function () { var apiDoc = new ApiDocumentation_1.default('Some summary\n@remarks and some remarks\n@public', extractor.docItemLoader, extractor, console.log, warnings); }); describe('Documentation Parser Tests', function () { it('Should report errors', function () { /** * To view the expected errors see: * - testInputs/example2/folder/MyDocumentedClass (10 errors) */ chai_1.assert.equal(capturedErrors.length, 10); chai_1.assert.equal(capturedErrors[0].message, 'Cannot provide summary in JsDoc if @inheritdoc tag is given'); chai_1.assert.equal(capturedErrors[1].message, 'The JSDoc tag "@summary" is not supported in this context'); chai_1.assert.equal(capturedErrors[2].message, 'Unexpected text in JSDoc comment: "Mock class for testing JsDoc parser"'); chai_1.assert.equal(capturedErrors[3].message, 'Unknown JSDoc tag "@badJsDocTag"'); chai_1.assert.equal(capturedErrors[4].message, 'Unknown tag name for inline tag.'); chai_1.assert.equal(capturedErrors[5].message, 'Too few parameters for @link inline tag.'); chai_1.assert.equal(capturedErrors[6].message, 'Unexpected text in JSDoc comment: "can not contain a tag"'); chai_1.assert.equal(capturedErrors[7].message, 'More than one API Tag was specified'); chai_1.assert.equal(capturedErrors[8].message, 'API reference expression must be of the form: \'scopeName/packageName:exportName.memberName ' + '| display text\'where the \'|\' is required if a display text is provided'); chai_1.assert.equal(capturedErrors[9].message, 'inheritdoc source item is deprecated. Must provide @deprecated message or remove @inheritdoc inline tag.'); }); it('Should parse API tag', function () { var expecedApiTag = ApiDocumentation_1.ApiTag.Public; var actualDoc = myDocumentedClass ? myDocumentedClass.documentation : undefined; chai_1.assert.isObject(actualDoc); chai_1.assert.equal(actualDoc.apiTag, expecedApiTag); }); }); }); //# sourceMappingURL=ApiDocumentation.test.js.map