Yuki Takei 6 лет назад
Родитель
Сommit
ec4f0d6e25

+ 1 - 1
config/jest.config.js

@@ -17,7 +17,7 @@ module.exports = {
       displayName: 'server',
       testEnvironment: 'node',
       rootDir: '.',
-      setupFilesAfterEnv: ['<rootDir>/src/test/bootstrap.js'],
+      setupFilesAfterEnv: ['<rootDir>/src/test/setup.js'],
       testMatch: ['<rootDir>/src/test/**/*.test.js'],
       // A map from regular expressions to module names that allow to stub out resources with a single module
       moduleNameMapper: {

+ 2 - 4
src/server/crowi/index.js

@@ -106,9 +106,7 @@ Crowi.prototype.init = async function() {
 };
 
 Crowi.prototype.initForTest = async function() {
-  // await this.setupDatabase();
   await this.setupModels();
-  // await this.setupSessionConfig();
   await this.setupConfigManager();
 
   // // customizeService depends on AppService and XssService
@@ -250,9 +248,9 @@ Crowi.prototype.setupConfigManager = async function() {
 };
 
 Crowi.prototype.setupModels = async function() {
-    Object.keys(models).forEach((key) => {
+  Object.keys(models).forEach((key) => {
     return this.model(key, models[key](this));
-    });
+  });
 };
 
 Crowi.prototype.getIo = function() {

+ 0 - 18
src/test/bootstrap.js

@@ -1,18 +0,0 @@
-process.env.NODE_ENV = 'test';
-
-const express = require('express');
-
-const testDBUtil = {
-  async generateFixture(conn, model, fixture) {
-    if (conn.readyState === 0) {
-      throw new Error();
-    }
-    const Model = conn.model(model);
-    return Promise.all(fixture.map((entity) => {
-      return new Model(entity).save();
-    }));
-  },
-};
-
-global.express = express;
-global.testDBUtil = testDBUtil;

+ 15 - 42
src/test/models/config.test.js

@@ -1,55 +1,28 @@
-const utils = require('../utils.js');
+const mongoose = require('mongoose');
 
-/* global testDBUtil */
+const { getInstance } = require('../setup-crowi');
 
 describe('Config model test', () => {
-  // const conn = utils.mongoose.connection;
-  // const Config = utils.models.Config;
-
-  beforeAll((done) => {
-
-    const fixture = [
-      { ns: 'crowi', key: 'test:test', value: JSON.stringify('crowi test value') },
-      { ns: 'crowi', key: 'test:test2', value: JSON.stringify(11111) },
-      { ns: 'crowi', key: 'test:test3', value: JSON.stringify([1, 2, 3, 4, 5]) },
-      { ns: 'plugin', key: 'other:config', value: JSON.stringify('this is data') },
-    ];
+  // eslint-disable-next-line no-unused-vars
+  let crowi;
+  let Config;
 
+  beforeAll(async(done) => {
+    crowi = await getInstance();
     done();
+  });
 
-    // testDBUtil.generateFixture(conn, 'Config', fixture)
-    //   .then((configs) => {
-    //     done();
-    //   })
-    //   .catch(() => {
-    //     done(new Error('Skip this test.'));
-    //   });
+  beforeEach(async(done) => {
+    Config = mongoose.model('Config');
+    done();
   });
 
   describe('.CONSTANTS', () => {
-    test('Config has constants', () => {
-      expect('hoge').toBe('Open');
-      // expect(Config.SECURITY_REGISTRATION_MODE_OPEN).toBe('Open');
-      // expect(Config.SECURITY_REGISTRATION_MODE_RESTRICTED).toBe('Resricted');
-      // expect(Config.SECURITY_REGISTRATION_MODE_CLOSED).toBe('Closed');
+    test('Config has constants', async() => {
+      expect(Config.SECURITY_REGISTRATION_MODE_OPEN).toBe('Open');
+      expect(Config.SECURITY_REGISTRATION_MODE_RESTRICTED).toBe('Resricted');
+      expect(Config.SECURITY_REGISTRATION_MODE_CLOSED).toBe('Closed');
     });
   });
 
-  // describe('.loadAllConfig', () => {
-  //   test('Get config array', (done) => {
-  //     const Config = mongoose.model('Config');
-
-  //     Config.loadAllConfig((err, config) => {
-  //       expect(config.crowi).toBeInstanceOf(Object);
-  //       expect(config.crowi).toHaveProperty('test:test', 'crowi test value');
-  //       expect(config.crowi).toHaveProperty('test:test2', 11111);
-  //       expect(config.crowi).toHaveProperty('test:test3', [1, 2, 3, 4, 5]);
-
-  //       expect(config.plugin).toBeInstanceOf(Object);
-  //       expect(config.plugin).toHaveProperty('other:config', 'this is data');
-
-  //       done();
-  //     });
-  //   });
-  // });
 });

+ 27 - 0
src/test/setup-crowi.js

@@ -0,0 +1,27 @@
+const helpers = require('@commons/util/helpers');
+
+const Crowi = require('@server/crowi');
+
+let _instance = null;
+
+async function createInstance() {
+  const instance = new Crowi(helpers.root());
+  await instance.initForTest();
+  return instance;
+}
+
+async function getInstance(isNewInstance) {
+  if (isNewInstance) {
+    return createInstance();
+  }
+
+  // initialize singleton instance
+  if (_instance == null) {
+    _instance = await createInstance();
+  }
+  return _instance;
+}
+
+module.exports = {
+  getInstance,
+};

+ 19 - 0
src/test/setup.js

@@ -0,0 +1,19 @@
+
+const mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || process.env.MONGO_URI || 'mongodb://localhost/growi_test';
+
+const mongoose = require('mongoose');
+
+mongoose.Promise = global.Promise;
+
+beforeAll(async(done) => {
+  await mongoose.connect(mongoUri, { useNewUrlParser: true });
+  await mongoose.connection.dropDatabase();
+  done();
+});
+
+afterAll(async(done) => {
+  await mongoose.disconnect();
+  done();
+});
+
+module.exports = {};

+ 0 - 40
src/test/utils.js

@@ -1,40 +0,0 @@
-
-const mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || process.env.MONGO_URI || 'mongodb://localhost/growi_test';
-
-const mongoose = require('mongoose');
-
-const models = require('@server/models');
-models.Config = require('@server/models/config');
-
-const helpers = require('@commons/util/helpers');
-const crowi = new (require('@server/crowi'))(helpers.root());
-
-mongoose.Promise = global.Promise;
-
-beforeAll(async() => {
-  if (!mongoUri) {
-    return;
-  }
-
-  await mongoose.connect(mongoUri, { useNewUrlParser: true });
-  await mongoose.connection.dropDatabase();
-});
-
-afterAll(async() => {
-  if (!mongoUri) {
-    return;
-  }
-
-  return mongoose.disconnect();
-});
-
-// Setup Models
-// for (const [modelName, model] of Object.entries(models)) {
-//   models[modelName] = model(crowi);
-// }
-// crowi.models = models;
-
-module.exports = {
-  models,
-  mongoose,
-};