Skip to content

Commit 366efe1

Browse files
committed
add plugin test and integration test stub, refactor templates and code to lib folder
1 parent 15079c8 commit 366efe1

20 files changed

+364
-80
lines changed

.npmignore

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Node template
3+
# Logs
4+
logs
5+
*.log
6+
npm-debug.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# node-waf configuration
27+
.lock-wscript
28+
29+
# Compiled binary addons (http://nodejs.org/api/addons.html)
30+
build/Release
31+
32+
# Dependency directories
33+
node_modules
34+
jspm_packages
35+
36+
# Optional npm cache directory
37+
.npm
38+
39+
# Optional eslint cache
40+
.eslintcache
41+
42+
# Optional REPL history
43+
.node_repl_history
44+
45+
# Output of 'npm pack'
46+
*.tgz
47+
48+
# Yarn Integrity file
49+
.yarn-integrity
50+
51+

__tests__/integration.test.wip.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
});

__tests__/plugin.test.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const fse = require('fs-extra');
5+
const Plugin = require('../index');
6+
7+
describe('plugin', () => {
8+
beforeAll(() => {
9+
const tmp = path.join(__dirname, '../', 'tmp');
10+
fse.mkdirsSync(tmp);
11+
process.chdir(tmp);
12+
});
13+
14+
it('checks that commands exists', () => {
15+
const plugin = new Plugin({}, {});
16+
const commands = Object.keys(plugin.commands);
17+
expect(commands).toEqual(['create', 'invoke']);
18+
});
19+
20+
it('checks hooks exists', () => {
21+
const plugin = new Plugin({}, {});
22+
const hooks = Object.keys(plugin.hooks);
23+
expect(hooks).toEqual([
24+
'create:test:test',
25+
'invoke:test:test',
26+
'create:function:create',
27+
]);
28+
});
29+
});

__tests__/test-service/.npmignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# package directories
2+
node_modules
3+
jspm_packages
4+
5+
# Serverless directories
6+
.serverless
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Proxy
2+
const path = require('path');
3+
const plugin = path.join(process.env.PLUGIN_TEST_DIR, '../', 'index.js');
4+
module.exports = require(plugin);

__tests__/test-service/handler.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
module.exports.hello = (event, context, callback) => {
4+
const response = {
5+
statusCode: 200,
6+
body: JSON.stringify({
7+
message: 'Go Serverless v1.0! Your function executed successfully!',
8+
input: event,
9+
}),
10+
};
11+
12+
callback(null, response);
13+
14+
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
15+
// callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
16+
};

__tests__/test-service/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "application-name",
3+
"version": "0.0.1"
4+
}

__tests__/test-service/serverless.yml

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Welcome to Serverless!
2+
#
3+
# This file is the main config file for your service.
4+
# It's very minimal at this point and uses default values.
5+
# You can always add more config options for more control.
6+
# We've included some commented out config examples here.
7+
# Just uncomment any of them to get that config option.
8+
#
9+
# For full config options, check the docs:
10+
# docs.serverless.com
11+
#
12+
# Happy Coding!
13+
14+
service: jest-test-suite
15+
16+
# You can pin your service to only deploy with a specific Serverless version
17+
# Check out our docs for more details
18+
# frameworkVersion: "=X.X.X"
19+
20+
provider:
21+
name: aws
22+
runtime: nodejs4.3
23+
24+
# you can overwrite defaults here
25+
# stage: dev
26+
# region: us-east-1
27+
28+
# you can add statements to the Lambda function's IAM Role here
29+
# iamRoleStatements:
30+
# - Effect: "Allow"
31+
# Action:
32+
# - "s3:ListBucket"
33+
# Resource: { "Fn::Join" : ["", ["arn:aws:s3:::", { "Ref" : "ServerlessDeploymentBucket" } ] ] }
34+
# - Effect: "Allow"
35+
# Action:
36+
# - "s3:PutObject"
37+
# Resource:
38+
# Fn::Join:
39+
# - ""
40+
# - - "arn:aws:s3:::"
41+
# - "Ref" : "ServerlessDeploymentBucket"
42+
43+
# you can define service wide environment variables here
44+
# environment:
45+
# variable1: value1
46+
47+
# you can add packaging information here
48+
#package:
49+
# include:
50+
# - include-me.js
51+
# - include-me-dir/**
52+
# exclude:
53+
# - exclude-me.js
54+
# - exclude-me-dir/**
55+
56+
functions:
57+
hello:
58+
handler: handler.hello
59+
60+
# The following are a few example events you can configure
61+
# NOTE: Please make sure to change your handler code to work with those events
62+
# Check the event documentation for details
63+
# events:
64+
# - http:
65+
# path: users/create
66+
# method: get
67+
# - s3: ${env:BUCKET}
68+
# - schedule: rate(10 minutes)
69+
# - sns: greeter-topic
70+
# - stream: arn:aws:dynamodb:region:XXXXXX:table/foo/stream/1970-01-01T00:00:00.000
71+
72+
# Define function environment variables here
73+
# environment:
74+
# variable2: value2
75+
76+
# you can add CloudFormation resource templates here
77+
#resources:
78+
# Resources:
79+
# NewResource:
80+
# Type: AWS::S3::Bucket
81+
# Properties:
82+
# BucketName: my-new-bucket
83+
# Outputs:
84+
# NewOutput:
85+
# Description: "Description for the output"
86+
# Value: "Some output value"
87+
88+
plugins:
89+
- serverless-jest-plugin

__tests__/testUtils.js __tests__/test-utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const replaceTextInFile = (filePath, subString, newSubString) => {
1111
};
1212

1313
const getTmpDirPath = () => path.join(os.tmpdir(),
14-
'tmpdirs-serverless-mocha-plugin',
15-
'serverless-mocha-plugin',
14+
'tmpdirs-serverless-jest-plugin',
15+
'serverless-jest-plugin',
1616
crypto.randomBytes(8).toString('hex'));
1717

1818
module.exports = {

__tests__/utils.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
const path = require('path');
44
const fse = require('fs-extra');
5-
const utils = require('../utils.js');
6-
const testUtils = require('./testUtils');
5+
const utils = require('../lib/utils.js');
6+
const testUtils = require('../__tests__/test-utils');
77

88
describe('utils', () => {
99
beforeAll(() => {
10-
process.env.MOCHA_PLUGIN_TEST_DIR = path.join(__dirname);
10+
process.env.PLUGIN_TEST_DIR = path.join(__dirname);
1111
const tmp = testUtils.getTmpDirPath();
1212
fse.mkdirsSync(tmp);
1313
process.chdir(tmp);
@@ -35,7 +35,7 @@ describe('utils', () => {
3535

3636
it('gets template from a file', () => {
3737
const templatePath =
38-
path.join(process.env.MOCHA_PLUGIN_TEST_DIR, '../', 'templates/test-template.ejs');
38+
path.join(process.env.PLUGIN_TEST_DIR, '../', 'lib', 'templates', 'test-template.ejs');
3939
const expectedTemplate = fse.readFileSync(templatePath, 'utf-8');
4040
const template = utils.getTemplateFromFile(templatePath);
4141
expect(template).toBe(expectedTemplate);

index.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
const BbPromise = require('bluebird');
44

5-
const createFunction = require('./create-function');
6-
const createTest = require('./create-test');
7-
const runTests = require('./run-tests');
5+
const createFunction = require('./lib/create-function');
6+
const createTest = require('./lib/create-test');
7+
const runTests = require('./lib/run-tests');
88

99
class ServerlessJestPlugin {
1010
constructor(serverless, options) {
@@ -14,7 +14,7 @@ class ServerlessJestPlugin {
1414
create: {
1515
commands: {
1616
test: {
17-
usage: 'Create mocha tests for service / function',
17+
usage: 'Create jest tests for service / function',
1818
lifecycleEvents: [
1919
'test',
2020
],
@@ -54,7 +54,7 @@ class ServerlessJestPlugin {
5454
},
5555
},
5656
invoke: {
57-
usage: 'Invoke mocha tests for service / function',
57+
usage: 'Invoke jest tests for service / function',
5858
commands: {
5959
test: {
6060
usage: 'Invoke test(s)',
@@ -67,11 +67,11 @@ class ServerlessJestPlugin {
6767
shortcut: 'f',
6868
},
6969
reporter: {
70-
usage: 'Mocha reporter to use',
70+
usage: 'Jest reporter to use',
7171
shortcut: 'R',
7272
},
7373
'reporter-options': {
74-
usage: 'Options for mocha reporter',
74+
usage: 'Options for jest reporter',
7575
shortcut: 'O',
7676
},
7777
path: {
@@ -101,4 +101,5 @@ class ServerlessJestPlugin {
101101
}
102102
}
103103

104-
module.exports = ServerlessJestPlugin;
104+
module.exports = ServerlessJestPlugin;
105+
module.exports.lambdaWrapper = require('lambda-wrapper');

jest-config.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"rootDir": "__tests__",
3-
"testRegex": ".*.test.js",
2+
"testRegex": ".+\\.test\\.js",
43
"testEnvironment": "node"
54
}

0 commit comments

Comments
 (0)