Skip to content

Testing and Typescript declaration mergingΒ #354

@francisdaigle

Description

@francisdaigle

πŸ› Bug Report

  • Plugin declarations not being merged as expected
  • Possibly related to plugin load order
❯ npm test

> fastify@1.0.0 test
> npm run build:ts && tsc -p test/tsconfig.test.json && tap test/**/*.test.ts


> fastify@1.0.0 build:ts
> tsc

 PASS  test/plugins/support.test.ts 1 OK 24.624ms
 FAIL  test/routes/example.test.ts
 βœ– β¨― Unable to compile TypeScript: src/plugins/aaa.ts(7,33): error TS2339: Property 'someSupport' does not exist on type 'FastifyInstance<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>'.

  test: example is loaded
  stack: |
    createTSError (node_modules/ts-node/src/index.ts:434:12)
    reportTSError (node_modules/ts-node/src/index.ts:438:19)
    getOutput (node_modules/ts-node/src/index.ts:578:36)
    Object.compile (node_modules/ts-node/src/index.ts:775:32)
    Module.m._compile (node_modules/ts-node/src/index.ts:858:43)
    require.extensions.<computed> (node_modules/ts-node/src/index.ts:861:12)
    Object.<anonymous> (node_modules/append-transform/index.js:62:4)
  at:
    line: 434
    column: 12
    file: node_modules/ts-node/src/index.ts
    function: createTSError
  type: TSError
  diagnosticText: >
    src/plugins/aaa.ts(7,33): error TS2339: Property 'someSupport' does not exist
    on type 'FastifyInstance<Server, IncomingMessage, ServerResponse,
    FastifyLoggerInstance>'.
  diagnosticCodes:
    - 2339
  tapCaught: returnedPromiseRejection

 FAIL  test/routes/root.test.ts
 βœ– β¨― Unable to compile TypeScript: src/plugins/aaa.ts(7,33): error TS2339: Property 'someSupport' does not exist on type 'FastifyInstance<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>'.

  test: default root route
  stack: |
    createTSError (node_modules/ts-node/src/index.ts:434:12)
    reportTSError (node_modules/ts-node/src/index.ts:438:19)
    getOutput (node_modules/ts-node/src/index.ts:578:36)
    Object.compile (node_modules/ts-node/src/index.ts:775:32)
    Module.m._compile (node_modules/ts-node/src/index.ts:858:43)
    require.extensions.<computed> (node_modules/ts-node/src/index.ts:861:12)
    Object.<anonymous> (node_modules/append-transform/index.js:62:4)
  at:
    line: 434
    column: 12
    file: node_modules/ts-node/src/index.ts
    function: createTSError
  type: TSError
  diagnosticText: >
    src/plugins/aaa.ts(7,33): error TS2339: Property 'someSupport' does not exist
    on type 'FastifyInstance<Server, IncomingMessage, ServerResponse,
    FastifyLoggerInstance>'.
  diagnosticCodes:
    - 2339
  tapCaught: returnedPromiseRejection

 FAIL  test/routes/root.test.ts 1 failed of 1 714.481ms
 βœ– β¨― Unable to compile TypeScript: src/plugins/aaa.ts(7,33): error TS2339: Property 'someSupport' does not exist on type 'FastifyInstance<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>'.

 FAIL  test/routes/example.test.ts 1 failed of 1 727.771ms
 βœ– β¨― Unable to compile TypeScript: src/plugins/aaa.ts(7,33): error TS2339: Property 'someSupport' does not exist on type 'FastifyInstance<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>'.


                         
  🌈 SUMMARY RESULTS 🌈  
                         

 FAIL  test/routes/example.test.ts 1 failed of 1 727.771ms
 βœ– β¨― Unable to compile TypeScript: src/plugins/aaa.ts(7,33): error TS2339: Property 'someSupport' does not exist on type 'FastifyInstance<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>'.

 FAIL  test/routes/root.test.ts 1 failed of 1 714.481ms
 βœ– β¨― Unable to compile TypeScript: src/plugins/aaa.ts(7,33): error TS2339: Property 'someSupport' does not exist on type 'FastifyInstance<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>'.

Suites:   2 failed, 1 passed, 3 of 3 completed
Asserts:  2 failed, 1 passed, of 3
Time:     3s
--------------|----------|----------|----------|----------|-------------------|
File          |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
--------------|----------|----------|----------|----------|-------------------|
All files     |       80 |      100 |       50 |       80 |                   |
 src          |      100 |      100 |      100 |      100 |                   |
  app.ts      |      100 |      100 |      100 |      100 |                   |
 src/plugins  |    69.23 |      100 |       40 |    69.23 |                   |
  sensible.ts |       75 |      100 |        0 |       75 |                10 |
  support.ts  |      100 |      100 |      100 |      100 |                   |
  zzz.ts      |       40 |      100 |        0 |       40 |             6,7,8 |
--------------|----------|----------|----------|----------|-------------------|

To Reproduce

  1. Add plugins:
// src/plugins/aaa.ts

import fp from "fastify-plugin";

export default fp(async (fastify, opts) => {
  fastify.decorate("aaa", function () {
    console.log(`aaa: ${fastify.someSupport()}`);
    return "aaa";
  });
});

declare module "fastify" {
  export interface FastifyInstance {
    aaa(): string;
  }
}
// src/plugins/zzz.ts

import fp from "fastify-plugin";

export default fp(async (fastify, opts) => {
  fastify.decorate("zzz", function () {
    console.log(`zzz: ${fastify.someSupport()}`);
    return "zzz";
  });
});

declare module "fastify" {
  export interface FastifyInstance {
    zzz(): string;
  }
}
  1. npm test

Expected behavior

❯ npm test

> fastify@1.0.0 test
> npm run build:ts && tsc -p test/tsconfig.test.json && tap test/**/*.test.ts


> fastify@1.0.0 build:ts
> tsc

 PASS  test/plugins/support.test.ts 1 OK 18.072ms
 PASS  test/routes/root.test.ts 1 OK 862.72ms
 PASS  test/routes/example.test.ts 1 OK 867.332ms

                         
  🌈 SUMMARY RESULTS 🌈  
                         

Suites:   3 passed, 3 of 3 completed
Asserts:  3 passed, of 3
Time:     3s

...

Your Environment

  • node version: 14.16.0
  • fastify version: 3.0.0
  • os: Mac

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions