【问题标题】:Strapi Unit TestingStrapi 单元测试
【发布时间】:2020-11-03 09:49:27
【问题描述】:

对于 Strapi 单元测试(使用 mysql 作为数据库),我遵循了官方文档,当我使用 npm run test 运行相同的测试时,我收到如下错误

my-project@0.1.0 测试 D:\Gerrit\website_backend\TE_MCA_DMSWEBV2 开玩笑 --forceExit --detectOpenHandles

● process.exit 以“1”调用

  at Strapi.stop (node_modules/strapi/lib/Strapi.js:238:13)
  at Strapi.stopWithError (node_modules/strapi/lib/Strapi.js:224:17)
  at node_modules/strapi-connector-bookshelf/lib/mount-models.js:625:14

运行测试/app.test.js npm 错误!代码生命周期 npm 错误!错误号 1 npm 错误!我的项目@0.1.0 测试:jest --forceExit --detectOpenHandles npm 错误!退出状态 1 npm 错误! npm 错误!在 my-project@0.1.0 测试脚本中失败。 npm 错误!这可能不是 npm 的问题。上面可能还有额外的日志输出。

database.js

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'mysql',
        host: env('DATABASE_HOST', '127.0.0.1'),
        port: env.int('DATABASE_PORT', 3306),
        database: env('DATABASE_NAME', 'myDB'),
        username: env('DATABASE_USERNAME', 'root'),
        password: env('DATABASE_PASSWORD', ''),
        ssl: env.bool('DATABASE_SSL', false),
      },
      options: {}
    },
  },
});

test/database.json

{
  "defaultConnection": "default",
  "connections": {
    "default": {
      "connector": "bookshelf",
      "settings": {
        "client": "mysql",
        "filename": ".tmp/test.db"
      },
      "options": {
        "useNullAsDefault": true,
        "pool": {
          "min": 0,
          "max": 15
        }
      }
    }
  }
}

tests/app.test.js

const fs = require('fs');
const { setupStrapi } = require('./helpers/strapi');

/** this code is called once before any test is called */
beforeAll(async (done) => {
  await setupStrapi(); // singleton so it can be called many times
  done();
});

/** this code is called once before all the tested are finished */
afterAll(async (done) => {
  const dbSettings = strapi.config.get('database.connections.default.settings');

  //delete test database after all tests
  if (dbSettings && dbSettings.filename) {
    const tmpDbFile = `${__dirname}/../${dbSettings.filename}`;
    if (fs.existsSync(tmpDbFile)) {
      fs.unlinkSync(tmpDbFile);
    }
  }
  done();
});

it('strapi is defined', async (done) => {
  expect(strapi).toBeDefined();
  done();
});

tests/helpers/strapi.js

const Strapi = require('strapi');
const http = require('http');

let instance;

async function setupStrapi() {
  if (!instance) {
    /** the following code in copied from `./node_modules/strapi/lib/Strapi.js` */
    await Strapi().load();
    instance = strapi; // strapi is global now
    await instance.app
      .use(instance.router.routes()) // populate KOA routes
      .use(instance.router.allowedMethods()); // populate KOA methods

    instance.server = http.createServer(instance.app.callback());
  }
  return instance;
}
module.exports = { setupStrapi };

【问题讨论】:

    标签: javascript node.js unit-testing strapi


    【解决方案1】:

    它来自文件夹名称,在文档中它是 ./config/env/test/database.json,但在您的文章中是 ./config/environments /test/database.json.

    其实这取决于你的strapi版本,新版本中env

    在本期中,您将获得所有详细信息: issue on Strapi Github

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多