著者:しょっさん
プログラミング言語「JavaScript」の実行環境「Node.js」と「Express」フレームワークを使って、基本となるWebアプリの開発手法を習得しましょう。第3回は「テストファースト」(最初にテストプログラムを作る)を意識したテストコードの作成方法を解説します。
シェルスクリプトマガジン Vol.57は以下のリンク先でご購入できます。
| 1 2 3 4 5 6 7 8 | describe('=validate', () => {   // 検証された結果   it('should get the result is true', () => {     const result = book.validate(req.body);     expect(result).toBe(true);     expect(req.body.image_url).toBe('http://example.com/');   }); }); | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | beforeEach(() => {   req = {     body: {       book_title: 'title',       author: ' しょっさん',       publisher: 'USP 研究所',       image_uml: ''     },     params: {       id: 1     }   };   res = {     redirect: function () { },     render: function () { }   }; }); | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | create: function (req, res) {   module.exports.register_book(req.body)     .then(result => {       res.redirect(/books/${result.id});     }).catch(errors => {       res.render('error', {         message: ' エラーが発生しました.',         error: {           status: ' 本を登録できませんでした.',           stack: errors         }       });   }); }, | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | describe('=register_book', () => {   it('should get the result is book information', (done) => {     book.register_book(req.body).then(result => {       expect(result.book_title).toBe('title');       expect(result.id).toBeGreaterThanOrEqual(2);       expect(result.image_url).toBe('http://example.com/');       done();     }).catch(done.fail);   });   it('should catch an error', (done) => {     req.body.book_title = '';     book.register_book(req.body).then(done.fail)       .catch(result => {       expect(result).toEqual([' 本のタイトルが入っていません']);       done();     });   }); }); | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 'use strict'; module.exports = {   up: (queryInterface, Sequelize) => {     /*       Add altering commands here.       Return a promise to correctly handle asynchronicity.       Example:       return queryInterface.bulkInsert('Person', [{         name: 'John Doe',         isBetaMember: false       }], {});     */   },   down: (queryInterface, Sequelize) => {     /*       Add reverting commands here.       Return a promise to correctly handle asynchronicity.       Example:       return queryInterface.bulkDelete('Person', null, {});     */   } }; | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 'use strict'; module.exports = {   up: (queryInterface, Sequelize) => {     const models = require('../models');     return models.Library.bulkCreate([       {          id: 1,          book_title: ' シェルスクリプトマガジン vol.54',          author: ' しょっさん',          publisher: 'USP 研究所',          image_uml: 'https://uec.usp-lab.com/INFO/IMG/SHELLSCRIPTMAG_VOL54.JPG'       },       {          id: 2,          book_title: ' シェルスクリプトマガジン vol.55',          author: ' しょっさん',          publisher: 'USP 研究所',          image_uml: 'https://uec.usp-lab.com/INFO/IMG/SHELLSCRIPTMAG_VOL55.JPG'       }     ]);   },   down: (queryInterface, Sequelize) => {     return queryInterface.bulkDelete('libraries', null, {});   } }; | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // routing テスト const request = require('supertest'); const app = require('../app'); // GET の場合 describe('GET /books/', () => {   it('respond with http', (done) => {     request(app)       .get('/books/')       .set('Accept', 'text/html')       .expect(200, done);   }); }); //POST の場合 describe('POST /books/create', () => {   it('respond with http', (done) => {     request(app)       .post('/books/create')       .send({book_title: 'test'})       .set('Accept', 'text/html')       .expect(302, done);   }); }); | 
| 1 2 3 4 | "scripts": {   "start": "node ./bin/www",   "test": "jasmine" }, | 
| 1 2 3 4 5 6 7 8 9 | "scripts": {   "start": "node ./bin/www",   "migrate": "sequelize db:migrate",   "seed": "sequelize db:seed:all",   "build": "npm run migrate && npm run seed",   "clean": "sequelize db:seed:undo:all",   "retest": "npm run clean && npm run build && npm test",   "test": "jasmine" }, | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | language: node_js node_js: - "10.11" - "8.12" before_install: before_script:   - "npm install"   - "npm run build" script:   - "npm test" notifications: on_success: always on_failure: always | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | version: 2 jobs:   build:     docker:       - image: circleci/node:10.11     working_directory: ~/repo     steps:       - checkout       - restore_cache:         keys:         - v1-dependencies-{{ checksum "package.json" }}         - v1-dependencies-       - run: npm install       - run: npm run build       - save_cache:         paths:           - node_modules           key: v1-dependencies-{{ checksum "package.json" }}       - run: npm test |