Просмотр исходного кода

Merge pull request #5347 from weseek/feat/separate-v5-compatible-db-for-test

support: Separate DB for v4 and v5 test
Yuki Takei 4 лет назад
Родитель
Сommit
2030dc2dca

+ 2 - 1
.github/workflows/ci-app.yml

@@ -98,7 +98,8 @@ jobs:
     - name: yarn test
       working-directory: ./packages/app
       run: |
-        yarn test
+        yarn test:ci --selectProjects unit server
+        yarn test:ci --selectProjects server-v5
       env:
         MONGO_URI: mongodb://localhost:${{ job.services.mongodb.ports['27017'] }}/growi_test
 

+ 20 - 1
packages/app/jest.config.js

@@ -37,7 +37,26 @@ module.exports = {
 
       rootDir: '.',
       roots: ['<rootDir>'],
-      testMatch: ['<rootDir>/test/integration/**/*.test.ts', '<rootDir>/test/integration/**/*.test.js'],
+      testMatch: ['<rootDir>/test/integration/**/*.test.ts', '<rootDir>/test/integration/**/*.test.js',
+                  '?!<rootDir>/test/integration/**/v5.*.test.ts', '?!<rootDir>/test/integration/**/v5.*.test.js'],
+
+      testEnvironment: 'node',
+      globalSetup: '<rootDir>/test/integration/global-setup.js',
+      globalTeardown: '<rootDir>/test/integration/global-teardown.js',
+      setupFilesAfterEnv: ['<rootDir>/test/integration/setup.js'],
+
+      // Automatically clear mock calls and instances between every test
+      clearMocks: true,
+      moduleNameMapper: MODULE_NAME_MAPPING,
+    },
+    {
+      displayName: 'server-v5',
+
+      preset: 'ts-jest/presets/js-with-ts',
+
+      rootDir: '.',
+      roots: ['<rootDir>'],
+      testMatch: ['<rootDir>/test/integration/**/v5.*.test.ts', '<rootDir>/test/integration/**/v5.*.test.js'],
 
       testEnvironment: 'node',
       globalSetup: '<rootDir>/test/integration/global-setup.js',

+ 1 - 0
packages/app/package.json

@@ -38,6 +38,7 @@
     "lint:swagger2openapi": "node node_modules/.bin/oas-validate tmp/swagger.json",
     "lint": "run-p lint:*",
     "test": "cross-env NODE_ENV=test jest --passWithNoTests -- ",
+    "test:ci": "cross-env NODE_ENV=test jest",
     "prelint:eslint": "yarn resources:plugin",
     "prelint:swagger2openapi": "yarn openapi:v3",
     "reg:run": "reg-suit run",

+ 5 - 1
packages/app/src/migrations/20210913153942-migrate-slack-app-integration-schema.js

@@ -20,7 +20,11 @@ defaultSupportedCommandsNameForSingleUse.forEach((commandName) => {
 module.exports = {
   async up(db) {
     logger.info('Apply migration');
-    await mongoose.connect(getMongoUri(), mongoOptions);
+    // connect only if disconnected
+    // see: https://mongoosejs.com/docs/api/connection.html#connection_Connection-readyState
+    if (mongoose.connection.readyState === 0) {
+      await mongoose.connect(getMongoUri(), mongoOptions);
+    }
 
     const SlackAppIntegration = getModelSafely('SlackAppIntegration') || require('~/server/models/slack-app-integration')();
 

+ 12 - 8
packages/app/test/integration/global-setup.js

@@ -16,22 +16,26 @@ if (process.env.NODE_ENV !== 'test') {
   throw new Error('\'process.env.NODE_ENV\' must be \'test\'');
 }
 
-
-// eslint-disable-next-line @typescript-eslint/no-var-requires
-// const { getInstance } = require('./setup-crowi');
-
 module.exports = async() => {
   initMongooseGlobalSettings();
 
-  await mongoose.connect(getMongoUri(), mongoOptions);
+  mongoose.connect(getMongoUri(), mongoOptions);
 
   // drop database
   await mongoose.connection.dropDatabase();
 
   // init DB
-  // const crowi = await getInstance();
-  // const appService = crowi.appService;
-  // await appService.initDB();
+  const pageCollection = mongoose.connection.collection('pages');
+  const userCollection = mongoose.connection.collection('users');
+
+  // create global user & rootPage
+  const globalUser = (await userCollection.insertMany([{ name: 'globalUser', username: 'globalUser', email: 'globalUser@example.com' }]))[0];
+  await pageCollection.insertMany([{
+    path: '/',
+    grant: 1,
+    creator: globalUser,
+    lastUpdateUser: globalUser,
+  }]);
 
   await mongoose.disconnect();
 };

+ 1 - 3
packages/app/test/integration/migrations/20210913153942-migrate-slack-app-integration-schema.test.ts

@@ -1,6 +1,5 @@
 import mongoose from 'mongoose';
 import { Collection } from 'mongodb';
-import { getMongoUri, mongoOptions } from '@growi/core';
 
 const migrate = require('~/migrations/20210913153942-migrate-slack-app-integration-schema');
 
@@ -9,8 +8,7 @@ describe('migrate-slack-app-integration-schema', () => {
   let collection: Collection;
 
   beforeAll(async() => {
-    await mongoose.connect(getMongoUri(), mongoOptions);
-    collection = mongoose.connection.db.collection('slackappintegrations');
+    collection = mongoose.connection.collection('slackappintegrations');
 
     await collection.insertMany([
       {

+ 0 - 6
packages/app/test/integration/service/page-grant.test.js

@@ -109,12 +109,6 @@ describe('PageGrantService', () => {
     ]);
 
     // Root page (Depth: 0)
-    await Page.insertMany([
-      {
-        path: '/',
-        grant: Page.GRANT_PUBLIC,
-      },
-    ]);
     rootPage = await Page.findOne({ path: '/' });
 
     // Empty pages (Depth: 1)

+ 2 - 0
packages/app/test/integration/service/v5-migration.test.js → packages/app/test/integration/service/v5.migration.test.js

@@ -16,6 +16,8 @@ describe('V5 page migration', () => {
     Page = mongoose.model('Page');
     User = mongoose.model('User');
 
+    await crowi.configManager.updateConfigsInTheSameNamespace('crowi', { 'app:isV5Compatible': true });
+
     await User.insertMany([{ name: 'testUser1', username: 'testUser1', email: 'testUser1@example.com' }]);
     testUser1 = await User.findOne({ username: 'testUser1' });
   });