Explorar o código

convirt mocha test files to jest

Yuki Takei %!s(int64=6) %!d(string=hai) anos
pai
achega
cebd79de1f

+ 11 - 27
src/test/bootstrap.js

@@ -2,41 +2,25 @@ process.env.NODE_ENV = 'test';
 
 require('module-alias/register');
 
-const helpers = require('@commons/util/helpers');
-
 const express = require('express');
+const path = require('path');
+
+const ROOT_DIR = path.join(__dirname, './../..');
+const MODEL_DIR = path.join(__dirname, './../server/models');
 
 const testDBUtil = {
-  generateFixture(conn, model, fixture) {
+  async generateFixture(conn, model, fixture) {
     if (conn.readyState === 0) {
-      return Promise.reject();
+      throw new Error();
     }
-    const m = conn.model(model);
-
-    return new Promise(((resolve) => {
-      const createdModels = [];
-      fixture.reduce((promise, entity) => {
-        return promise.then(() => {
-          const newDoc = new m(); // eslint-disable-line new-cap
-
-          Object.keys(entity).forEach((k) => {
-            newDoc[k] = entity[k];
-          });
-          return new Promise(((r) => {
-            newDoc.save((err, data) => {
-              createdModels.push(data);
-              return r();
-            });
-          }));
-        });
-      }, Promise.resolve()).then(() => {
-        resolve(createdModels);
-      });
+    const Model = conn.model(model);
+    return Promise.all(fixture.map((entity) => {
+      return new Model(entity).save();
     }));
   },
 };
 
 global.express = express;
-global.ROOT_DIR = helpers.root();
-global.MODEL_DIR = helpers.root('src/server/models');
+global.ROOT_DIR = ROOT_DIR;
+global.MODEL_DIR = MODEL_DIR;
 global.testDBUtil = testDBUtil;

+ 3 - 3
src/test/crowi/crowi.test.js

@@ -12,21 +12,21 @@ describe('Test for Crowi application context', () => {
 
   const mongoose = require('mongoose');
   describe('construction', () => {
-    it('initialize crowi context', () => {
+    test('initialize crowi context', () => {
       const crowi = new Crowi(helpers.root());
       expect(crowi).to.be.instanceof(Crowi);
       expect(crowi.version).to.equal(require('../../../package.json').version);
       expect(crowi.env).to.be.an('Object');
     });
 
-    it('config getter, setter', () => {
+    test('config getter, setter', () => {
       const crowi = new Crowi(helpers.root());
       expect(crowi.getConfig()).to.deep.equals({});
       crowi.setConfig({ test: 1 });
       expect(crowi.getConfig()).to.deep.equals({ test: 1 });
     });
 
-    it('model getter, setter', () => {
+    test('model getter, setter', () => {
       const crowi = new Crowi(helpers.root());
       // set
       crowi.model('hoge', { fuga: 1 });

+ 3 - 3
src/test/models/config.test.js

@@ -16,7 +16,7 @@ describe('Config model test', () => {
 
   const conn = utils.mongoose.connection;
 
-  before((done) => {
+  beforeAll((done) => {
     const fixture = [
       { ns: 'crowi', key: 'test:test', value: JSON.stringify('crowi test value') },
       { ns: 'crowi', key: 'test:test2', value: JSON.stringify(11111) },
@@ -33,7 +33,7 @@ describe('Config model test', () => {
   });
 
   describe('.CONSTANTS', () => {
-    it('Config has constants', () => {
+    test('Config has constants', () => {
       expect(Config.SECURITY_REGISTRATION_MODE_OPEN).to.have.string('Open');
       expect(Config.SECURITY_REGISTRATION_MODE_RESTRICTED).to.have.string('Resricted');
       expect(Config.SECURITY_REGISTRATION_MODE_CLOSED).to.have.string('Closed');
@@ -41,7 +41,7 @@ describe('Config model test', () => {
   });
 
   describe('.loadAllConfig', () => {
-    it('Get config array', (done) => {
+    test('Get config array', (done) => {
       Config.loadAllConfig((err, config) => {
         expect(config.crowi).to.be.an('Object');
         expect(config.crowi).to.have.property('test:test')

+ 32 - 32
src/test/models/page.test.js

@@ -15,7 +15,7 @@ describe('Page', () => {
   let createdPages;
   let createdUsers;
 
-  before(async() => {
+  beforeAll(async() => {
     await conn.collection('pages').remove();
 
     const userFixture = [
@@ -113,8 +113,8 @@ describe('Page', () => {
   });
 
   describe('.isPublic', () => {
-    context('with a public page', () => {
-      it('should return true', (done) => {
+    describe('with a public page', () => {
+      test('should return true', (done) => {
         Page.findOne({ path: '/grant/public' }, (err, page) => {
           expect(err).to.be.null;
           expect(page.isPublic()).to.be.equal(true);
@@ -124,8 +124,8 @@ describe('Page', () => {
     });
 
     ['restricted', 'specified', 'owner'].forEach((grant) => {
-      context(`with a ${grant} page`, () => {
-        it('should return false', (done) => {
+      describe(`with a ${grant} page`, () => {
+        test('should return false', (done) => {
           Page.findOne({ path: `/grant/${grant}` }, (err, page) => {
             expect(err).to.be.null;
             expect(page.isPublic()).to.be.equal(false);
@@ -137,13 +137,13 @@ describe('Page', () => {
   });
 
   describe('.getDeletedPageName', () => {
-    it('should return trash page name', () => {
+    test('should return trash page name', () => {
       expect(Page.getDeletedPageName('/hoge')).to.be.equal('/trash/hoge');
       expect(Page.getDeletedPageName('hoge')).to.be.equal('/trash/hoge');
     });
   });
   describe('.getRevertDeletedPageName', () => {
-    it('should return reverted trash page name', () => {
+    test('should return reverted trash page name', () => {
       expect(Page.getRevertDeletedPageName('/hoge')).to.be.equal('/hoge');
       expect(Page.getRevertDeletedPageName('/trash/hoge')).to.be.equal('/hoge');
       expect(Page.getRevertDeletedPageName('/trash/hoge/trash')).to.be.equal('/hoge/trash');
@@ -151,7 +151,7 @@ describe('Page', () => {
   });
 
   describe('.isDeletableName', () => {
-    it('should decide deletable or not', () => {
+    test('should decide deletable or not', () => {
       expect(Page.isDeletableName('/hoge')).to.be.true;
       expect(Page.isDeletableName('/user/xxx')).to.be.false;
       expect(Page.isDeletableName('/user/xxx123')).to.be.false;
@@ -161,7 +161,7 @@ describe('Page', () => {
   });
 
   describe('.isCreatableName', () => {
-    it('should decide creatable or not', () => {
+    test('should decide creatable or not', () => {
       expect(Page.isCreatableName('/hoge')).to.be.true;
 
       // edge cases
@@ -205,8 +205,8 @@ describe('Page', () => {
   });
 
   describe('.isAccessiblePageByViewer', () => {
-    context('with a granted user', () => {
-      it('should return true', async() => {
+    describe('with a granted user', () => {
+      test('should return true', async() => {
         const user = await User.findOne({ email: 'anonymous0@example.com' });
         const page = await Page.findOne({ path: '/user/anonymous0/memo' });
 
@@ -215,8 +215,8 @@ describe('Page', () => {
       });
     });
 
-    context('with a public page', () => {
-      it('should return true', async() => {
+    describe('with a public page', () => {
+      test('should return true', async() => {
         const user = await User.findOne({ email: 'anonymous1@example.com' });
         const page = await Page.findOne({ path: '/grant/public' });
 
@@ -225,8 +225,8 @@ describe('Page', () => {
       });
     });
 
-    context('with a restricted page and an user who has no grant', () => {
-      it('should return false', async() => {
+    describe('with a restricted page and an user who has no grant', () => {
+      test('should return false', async() => {
         const user = await User.findOne({ email: 'anonymous1@example.com' });
         const page = await Page.findOne({ path: '/grant/owner' });
 
@@ -237,8 +237,8 @@ describe('Page', () => {
   });
 
   describe('Extended field', () => {
-    context('Slack Channel.', () => {
-      it('should be empty', (done) => {
+    describe('Slack Channel.', () => {
+      test('should be empty', (done) => {
         Page.findOne({ path: '/page/for/extended' }, (err, page) => {
           expect(page.extended.hoge).to.be.equal(1);
           expect(page.getSlackChannel()).to.be.equal('');
@@ -246,7 +246,7 @@ describe('Page', () => {
         });
       });
 
-      it('set slack channel and should get it and should keep hoge ', async() => {
+      test('set slack channel and should get it and should keep hoge ', async() => {
         let page = await Page.findOne({ path: '/page/for/extended' });
         await page.updateSlackChannel('slack-channel1');
         page = await Page.findOne({ path: '/page/for/extended' });
@@ -257,8 +257,8 @@ describe('Page', () => {
   });
 
   describe('.findPage', () => {
-    context('findByIdAndViewer', () => {
-      it('should find page (public)', async() => {
+    describe('findByIdAndViewer', () => {
+      test('should find page (public)', async() => {
         const pageToFind = createdPages[1];
         const grantedUser = createdUsers[0];
 
@@ -267,7 +267,7 @@ describe('Page', () => {
         expect(page.path).to.equal(pageToFind.path);
       });
 
-      it('should find page (anyone knows link)', async() => {
+      test('should find page (anyone knows link)', async() => {
         const pageToFind = createdPages[2];
         const grantedUser = createdUsers[1];
 
@@ -276,7 +276,7 @@ describe('Page', () => {
         expect(page.path).to.equal(pageToFind.path);
       });
 
-      it('should find page (just me)', async() => {
+      test('should find page (just me)', async() => {
         const pageToFind = createdPages[4];
         const grantedUser = createdUsers[0];
 
@@ -285,7 +285,7 @@ describe('Page', () => {
         expect(page.path).to.equal(pageToFind.path);
       });
 
-      it('should not be found by grant (just me)', async() => {
+      test('should not be found by grant (just me)', async() => {
         const pageToFind = createdPages[4];
         const grantedUser = createdUsers[1];
 
@@ -294,8 +294,8 @@ describe('Page', () => {
       });
     });
 
-    context('findByIdAndViewer granted userGroup', () => {
-      it('should find page', async() => {
+    describe('findByIdAndViewer granted userGroup', () => {
+      test('should find page', async() => {
         const pageToFind = createdPages[6];
         const grantedUser = createdUsers[0];
 
@@ -304,7 +304,7 @@ describe('Page', () => {
         expect(page.path).to.equal(pageToFind.path);
       });
 
-      it('should not be found by grant', async() => {
+      test('should not be found by grant', async() => {
         const pageToFind = createdPages[6];
         const grantedUser = createdUsers[2];
 
@@ -314,8 +314,8 @@ describe('Page', () => {
     });
   });
 
-  context('findListWithDescendants', () => {
-    it('should return only /page/', async() => {
+  describe('findListWithDescendants', () => {
+    test('should return only /page/', async() => {
       const user = createdUsers[0];
 
       const result = await Page.findListWithDescendants('/page/', user, { isRegExpEscapedFromPath: true });
@@ -326,7 +326,7 @@ describe('Page', () => {
       const pagePaths = result.pages.map((page) => { return page.path });
       expect(pagePaths).to.include.members(['/page/for/extended']);
     });
-    it('should return only /page1/', async() => {
+    test('should return only /page1/', async() => {
       const user = createdUsers[0];
 
       const result = await Page.findListWithDescendants('/page1/', user, { isRegExpEscapedFromPath: true });
@@ -339,8 +339,8 @@ describe('Page', () => {
     });
   });
 
-  context('findListByStartWith', () => {
-    it('should return pages which starts with /page', async() => {
+  describe('findListByStartWith', () => {
+    test('should return pages which starts with /page', async() => {
       const user = createdUsers[0];
 
       const result = await Page.findListByStartWith('/page', user, {});
@@ -351,7 +351,7 @@ describe('Page', () => {
       const pagePaths = result.pages.map((page) => { return page.path });
       expect(pagePaths).to.include.members(['/page/for/extended', '/page1', '/page1/child1', '/page2']);
     });
-    it('should process with regexp', async() => {
+    test('should process with regexp', async() => {
       const user = createdUsers[0];
 
       const result = await Page.findListByStartWith('/page\\d{1}/', user, {});

+ 6 - 6
src/test/models/updatePost.test.js

@@ -10,8 +10,8 @@ describe('UpdatePost', () => {
   const UpdatePost = utils.models.UpdatePost;
 
   describe('.createPrefixesByPathPattern', () => {
-    context('with a path', () => {
-      it('should return right patternPrfixes', (done) => {
+    describe('with a path', () => {
+      test('should return right patternPrfixes', (done) => {
         expect(UpdatePost.createPrefixesByPathPattern('/*')).to.deep.equal(['*', '*']);
         expect(UpdatePost.createPrefixesByPathPattern('/user/*/日報*')).to.deep.equal(['user', '*']);
         expect(UpdatePost.createPrefixesByPathPattern('/project/hoge/*')).to.deep.equal(['project', 'hoge']);
@@ -25,8 +25,8 @@ describe('UpdatePost', () => {
   });
 
   describe('.getRegExpByPattern', () => {
-    context('with a pattern', () => {
-      it('should return right regexp', (done) => {
+    describe('with a pattern', () => {
+      test('should return right regexp', (done) => {
         expect(UpdatePost.getRegExpByPattern('/*')).to.deep.equal(/^\/.*/);
         expect(UpdatePost.getRegExpByPattern('/user/*/日報*')).to.deep.equal(/^\/user\/.*\/日報.*/);
         expect(UpdatePost.getRegExpByPattern('/project/hoge/*')).to.deep.equal(/^\/project\/hoge\/.*/);
@@ -39,8 +39,8 @@ describe('UpdatePost', () => {
   });
 
   describe('.normalizeChannelName', () => {
-    context('with a channel name', () => {
-      it('should return true', (done) => {
+    describe('with a channel name', () => {
+      test('should return true', (done) => {
         expect(UpdatePost.normalizeChannelName('#pj-hoge')).to.be.equal('pj-hoge');
         expect(UpdatePost.normalizeChannelName('pj-hoge')).to.be.equal('pj-hoge');
 

+ 8 - 8
src/test/models/user.test.js

@@ -10,7 +10,7 @@ describe('User', () => {
   const conn = utils.mongoose.connection;
 
   // clear collection
-  before((done) => {
+  beforeAll((done) => {
     conn.collection('users').remove()
       .then(() => {
         done();
@@ -18,8 +18,8 @@ describe('User', () => {
   });
 
   describe('Create and Find.', () => {
-    context('The user', () => {
-      it('should created', (done) => {
+    describe('The user', () => {
+      test('should created', (done) => {
         User.createUserByEmailAndPassword('Aoi Miyazaki', 'aoi', 'aoi@example.com', 'hogefuga11', 'en', (err, userData) => {
           expect(err).to.be.null;
           expect(userData).to.instanceof(User);
@@ -27,7 +27,7 @@ describe('User', () => {
         });
       });
 
-      it('should be found by findUserByUsername', (done) => {
+      test('should be found by findUserByUsername', (done) => {
         User.findUserByUsername('aoi')
           .then((userData) => {
             expect(userData).to.instanceof(User);
@@ -35,7 +35,7 @@ describe('User', () => {
           });
       });
 
-      it('should be found by findUsersByPartOfEmail', (done) => {
+      test('should be found by findUsersByPartOfEmail', (done) => {
         User.findUsersByPartOfEmail('ao', {})
           .then((userData) => {
             expect(userData).to.instanceof(Array);
@@ -48,8 +48,8 @@ describe('User', () => {
   });
 
   describe('User Utilities', () => {
-    context('Get username from path', () => {
-      it('found', (done) => {
+    describe('Get username from path', () => {
+      test('found', (done) => {
         let username = null;
         username = User.getUsernameByPath('/user/sotarok');
         expect(username).to.equal('sotarok');
@@ -60,7 +60,7 @@ describe('User', () => {
         done();
       });
 
-      it('not found', (done) => {
+      test('not found', (done) => {
         let username = null;
         username = User.getUsernameByPath('/the/page/is/not/related/to/user/page');
         expect(username).to.be.null;

+ 2 - 2
src/test/util/slack.test.js

@@ -11,11 +11,11 @@ describe('Slack Util', () => {
   const crowi = new Crowi(helpers.root(), process.env);
   const slack = require(`${crowi.libDir}/util/slack`)(crowi);
 
-  it('post comment method exists', () => {
+  test('post comment method exists', () => {
     expect(slack).to.respondTo('postComment');
   });
 
-  it('post page method exists', () => {
+  test('post page method exists', () => {
     expect(slack).to.respondTo('postPage');
   });
 });

+ 3 - 2
src/test/utils.js

@@ -1,5 +1,6 @@
 
 const mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || process.env.MONGO_URI || 'mongodb://localhost/growi_test';
+
 const mongoose = require('mongoose');
 
 const helpers = require('@commons/util/helpers');
@@ -11,7 +12,7 @@ const models = {};
 
 mongoose.Promise = global.Promise;
 
-before('Create database connection and clean up', async() => {
+beforeAll(async() => {
   if (!mongoUri) {
     return;
   }
@@ -24,7 +25,7 @@ before('Create database connection and clean up', async() => {
   await crowi.initForTest();
 });
 
-after('Close database connection', async() => {
+afterAll(async() => {
   if (!mongoUri) {
     return;
   }