STRIPE + TypeScript:TypeError:stripe_1.default 不是构造函数

JAN*_*JAN 4 stripe-payments typescript jestjs

我正在 TypeScript MicroService 中运行 Stripe 代码的一些测试:

process.env.STRIPE_KEY = 'sk_test_...............';
import Stripe from 'stripe';
export const stripe = new Stripe(process.env.STRIPE_KEY!, {
  apiVersion: '2020-03-02',
});
Run Code Online (Sandbox Code Playgroud)

但是我不断发现构造函数未定义:

 **TypeError: stripe_1.default is not a constructor**

 1 | import Stripe from 'stripe';
> 2 | export const stripe = new Stripe(process.env.STRIPE_KEY!, {
    |                       ^
  3 |   apiVersion: '2020-03-02',
  4 | });
  5 |

  at Object.<anonymous> (src/stripe.ts:2:23)
  at Object.<anonymous> (src/routes/new.ts:14:1)
  at Object.<anonymous> (src/app.ts:6:1)
  at Object.<anonymous> (src/routes/__test__/newNoMocks.test.ts:3:1)
 
Run Code Online (Sandbox Code Playgroud)

包 JSON:

"dependencies": {    
    "@types/cookie-session": "^2.0.40",
    "@types/express": "^4.17.3",
    "@types/jsonwebtoken": "^8.5.0",
    "@types/mongoose": "^5.7.22",
    "cookie-session": "^1.4.0",
    "express": "^4.17.1",
    "express-async-errors": "^3.1.1",
    "express-validator": "^6.4.0",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.9.18",
    "mongoose-update-if-current": "^1.4.0",
    "stripe": "^8.81.0",
    "ts-node-dev": "^1.0.0-pre.44",
    "typescript": "^3.8.3"
  },
Run Code Online (Sandbox Code Playgroud)

知道什么可能会导致这种情况吗?

TSCONFIG:

{
  "compilerOptions": {
    /* Basic Options */
    // "incremental": true,                   /* Enable incremental compilation */
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    // "lib": [],                             /* Specify library files to be included in the compilation. */
    // "allowJs": true,                       /* Allow javascript files to be compiled. */
    // "checkJs": true,                       /* Report errors in .js files. */
    // "jsx": "preserve",                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
    "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    // "declarationMap": true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
    // "sourceMap": true,                     /* Generates corresponding '.map' file. */
    // "outFile": "./",                       /* Concatenate and emit output to single file. */
    "outDir": "./build",                        /* Redirect output structure to the directory. */
    // "rootDir": "./",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    // "composite": true,                     /* Enable project compilation */
    // "tsBuildInfoFile": "./",               /* Specify file to store incremental compilation information */
    // "removeComments": true,                /* Do not emit comments to output. */
    // "noEmit": true,                        /* Do not emit outputs. */
    // "importHelpers": true,                 /* Import emit helpers from 'tslib'. */
    // "downlevelIteration": true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
    // "isolatedModules": true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

    /* Strict Type-Checking Options */
    "strict": true,                           /* Enable all strict type-checking options. */
    // "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
    // "strictNullChecks": true,              /* Enable strict null checks. */
    // "strictFunctionTypes": true,           /* Enable strict checking of function types. */
    // "strictBindCallApply": true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
    // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    // "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
    // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */

    /* Additional Checks */
    // "noUnusedLocals": true,                /* Report errors on unused locals. */
    // "noUnusedParameters": true,            /* Report errors on unused parameters. */
    // "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
    // "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */

    /* Module Resolution Options */
    // "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    // "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
    // "paths": {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
    // "rootDirs": [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
    // "typeRoots": [],                       /* List of folders to include type definitions from. */
    // "types": [],                           /* Type declaration files to be included in compilation. */
    // "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    // "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */
    // "allowUmdGlobalAccess": true,          /* Allow accessing UMD globals from modules. */

    /* Source Map Options */
    // "sourceRoot": "",                      /* Specify the location where debugger should locate TypeScript files instead of source locations. */
    // "mapRoot": "",                         /* Specify the location where debugger should locate map files instead of generated locations. */
    // "inlineSourceMap": true,               /* Emit a single file with source maps instead of having a separate file. */
    // "inlineSources": true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

    /* Experimental Options */
    // "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
    // "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */
    /* Advanced Options */
    ,"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑 #2 - 02-08-20 - 测试:

新的.test.ts:

import mongoose from 'mongoose';
import request from 'supertest';
import { app } from '../../app';
import { Order } from '../../models/order';
import { OrderStatus } from '@myNPMpackage/common';
import { stripe } from '../../stripe';
import { Payment } from '../../models/payment';

it('No Mock - returns a 201 with valid inputs', async () => {
  const userId = mongoose.Types.ObjectId().toHexString();
  const price = Math.floor(Math.random() * 100000);
  // create order
  const order = Order.build({
    id: mongoose.Types.ObjectId().toHexString(),
    userId,
    version: 0,
    price,
    status: OrderStatus.Created,
  });

  await order.save();

  // send API Request
  await request(app)
    .post('/api/payments')
    .set('Cookie', global.signin(userId))
    .send({
      orderId: order.id,
      token: 'tok_visa', // valid token
    })
    .expect(201);

  const stripeCharges = await stripe.charges.list({ limit: 50 });
  const stripeCharge = stripeCharges.data.find((charge) => {
    return charge.amount === price * 100; // *100 because we move from Dollars to Cents
  });
  expect(stripeCharge).toBeDefined();
  expect(stripeCharge!.currency).toEqual('usd');

  // Test Payment
  const payment = await Payment.findOne({
    orderId: order.id,
    stripeId: stripeCharge!.id,
  });

  expect(payment).not.toBeNull();
});
Run Code Online (Sandbox Code Playgroud)

路由器

router.post(
  '/api/payments',
  requireAuth,
  [body('token').not().isEmpty(), body('orderId').not().isEmpty()],
  validateRequest,
  async (req: Request, res: Response) => {
    const { token, orderId } = req.body;
    const order = await Order.findById(orderId);
    if (!order) {
      throw new NotFoundError();
    }
    if (order.userId !== req.currentUser!.id) {
      throw new NotAuthorizedError();
    }

    if (order.status === OrderStatus.Cancelled) {
      throw new BadRequestError('Cannot pay for a cancelled order!');
    }

    // Call Stripe and charge Credit Cards
    const charge = await stripe.charges.create({
      currency: 'usd',
      amount: order.price * 100, // given in USD , we need to convert to CENTS
      source: token,
    });

    // Create a payment
    const payment = Payment.build({
      orderId,
      stripeId: charge.id,
    });

    // Save the payment
    await payment.save();

    // Call publisher
    await new PaymentCreatedPublisher(natsWrapper.client).publish({
      id: payment.id,
      orderId: payment.orderId,
      stripeId: payment.stripeId,
    });

    res.status(201).send(payment);
  }
);
Run Code Online (Sandbox Code Playgroud)

应用程序.ts:

import express from 'express';
import 'express-async-errors';
import { json } from 'body-parser';
import cookieSession from 'cookie-session';
import { errorHandler, NotFoundError, currentUser } from '@myNPMpackage/common';
import { createChargeRouter } from './routes/new';

const app = express();
app.set('trust proxy', true); // express is aware that it's behind a proxy inginx and it should trust traffic from that proxy
app.use(json());
app.use(
  cookieSession({
    signed: false,
    secure: false, // this one means - don't send cookie if the request is not HTTPS (Secured) , HTTP request won't receive the cookie
    // secure : process.env.NODE_ENV !== 'test'
  })
);

app.use(currentUser);
app.use(createChargeRouter);

app.all('*', async (req, res, next) => {
  // next(new NotFoundError());
  throw new NotFoundError();
});

app.use(errorHandler);

export { app };
Run Code Online (Sandbox Code Playgroud)

par*_*axi 8

我怀疑您正在学习Stephen Grider 的微服务课程

您重命名src/__mocks__/stripe.ts为吗src/__mocks__/stripe.ts.old

这看起来 TS 正在针对模拟条带模块而不是真正的条带模块工作。确保删除(或重命名)src/__mocks__/stripe.ts

如果模拟条带文件名与模块相同,则 jest 将使用模拟条带。请注意,即使您删除了,这也适用 jest.mock('../../stripe');

来自笑话文档

当给定模块存在手动模拟时,Jest 的模块系统将在显式调用 jest.mock('moduleName') 时使用该模块。但是,当 automock 设置为 true 时,即使没有调用 jest.mock('moduleName') ,也会使用手动模拟实现而不是自动创建的模拟。要选择退出此行为,您需要在应使用实际模块实现的测试中显式调用 jest.unmock('moduleName') 。