|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Serverless = require('serverless'); |
| 4 | +const execSync = require('child_process').execSync; |
| 5 | +const path = require('path'); |
| 6 | +const fse = require('fs-extra'); |
| 7 | +const testUtils = require('./test-utils'); |
| 8 | + |
| 9 | +const serverless = new Serverless(); |
| 10 | +serverless.init(); |
| 11 | +const serverlessExec = path.join(serverless.config.serverlessPath, '..', 'bin', 'serverless'); |
| 12 | + |
| 13 | +describe('integration', () => { |
| 14 | + beforeAll(() => { |
| 15 | + process.env.PLUGIN_TEST_DIR = path.join(__dirname); |
| 16 | + const tmpDir = testUtils.getTmpDirPath(); |
| 17 | + fse.mkdirsSync(tmpDir); |
| 18 | + fse.copySync(path.join(process.env.PLUGIN_TEST_DIR, 'test-service'), tmpDir); |
| 19 | + process.chdir(tmpDir); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should contain test params in cli info', () => { |
| 23 | + const test = execSync(`${serverlessExec}`); |
| 24 | + const result = new Buffer(test, 'base64').toString(); |
| 25 | + expect(result).toContain( |
| 26 | + 'create test ................... Create jest tests for service / function' |
| 27 | + ); |
| 28 | + expect(result).toContain( |
| 29 | + 'create function ............... Create a function into the service' |
| 30 | + ); |
| 31 | + expect(result).toContain( |
| 32 | + 'invoke test ................... Invoke test(s)' |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should create test for hello function', () => { |
| 37 | + const test = execSync(`${serverlessExec} create test --function hello`); |
| 38 | + const result = new Buffer(test, 'base64').toString(); |
| 39 | + expect(result).toContain( |
| 40 | + 'serverless-jest-plugin: created hello.test.js' |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should create function goodbye', () => { |
| 45 | + const test = execSync( |
| 46 | + `${serverlessExec} create function --function goodbye --handler goodbye/index.handler` |
| 47 | + ); |
| 48 | + const result = new Buffer(test, 'base64').toString(); |
| 49 | + expect(result).toContain( |
| 50 | + 'serverless-jest-plugin: created goodbye/goodbye.test.js' |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + |
| 55 | + it('should run tests successfully', () => { |
| 56 | + // change test files to use local proxy version of mocha plugin |
| 57 | + testUtils.replaceTextInFile( |
| 58 | + path.join('hello.test.js'), |
| 59 | + 'require(\'serverless-jest-plugin\')', |
| 60 | + 'require(\'../.serverless_plugins/serverless-jest-plugin/index.js\')' |
| 61 | + ); |
| 62 | + testUtils.replaceTextInFile( |
| 63 | + path.join('goodbye', 'goodbye.test.js'), |
| 64 | + 'require(\'serverless-jest-plugin\')', |
| 65 | + 'require(\'../.serverless_plugins/serverless-jest-plugin/index.js\')' |
| 66 | + ); |
| 67 | + const test = execSync(`${serverlessExec} invoke test`); |
| 68 | + const result = new Buffer(test, 'base64').toString(); |
| 69 | + expect(result).toContain( |
| 70 | + 'goodbye\n ✓ implement tests here' |
| 71 | + ); |
| 72 | + |
| 73 | + expect(result).toContain( |
| 74 | + 'hello\n ✓ implement tests here' |
| 75 | + ); |
| 76 | + |
| 77 | + expect(result).toContain( |
| 78 | + '2 passing' |
| 79 | + ); |
| 80 | + }); |
| 81 | +}); |
0 commit comments