Skip to content

Commit f9a4595

Browse files
authored
Merge pull request #6 from codeceptjs/move-to-ts
2 parents 12acde4 + 9b3846e commit f9a4595

File tree

8 files changed

+202
-50
lines changed

8 files changed

+202
-50
lines changed

.github/workflows/publish-node.js.yml

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
1-
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
1+
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
22
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3-
4-
name: Publish
3+
name: Publish npm Package
54

65
on:
7-
release:
8-
types: [published]
9-
jobs:
10-
publish:
6+
push:
7+
branches:
8+
- master
9+
- main
1110

11+
jobs:
12+
publish-npm:
1213
runs-on: ubuntu-latest
13-
1414
steps:
15-
- uses: actions/checkout@v2
16-
with:
17-
ref: ${{ github.event.release.target_commitish }}
18-
- uses: actions/setup-node@v2
19-
with:
20-
node-version: 16
21-
cache: 'npm'
22-
- run: git config --global user.name "GitHub CD bot"
23-
- run: git config --global user.email "[email protected]"
24-
- run: npm version ${{ github.event.release.tag_name }}
25-
- uses: JS-DevTools/npm-publish@v1
26-
with:
27-
token: ${{ secrets.NPM_TOKEN }}
28-
29-
# push the version changes to GitHub
30-
- run: git push
31-
env:
32-
# The secret is passed automatically. Nothing to configure.
33-
github-token: ${{ secrets.GITHUB_TOKEN }}
15+
- uses: actions/checkout@v3
16+
- uses: actions/setup-node@v3
17+
with:
18+
node-version: 20
19+
registry-url: https://registry.npmjs.org/
20+
- run: git config --global user.name "GitHub CD bot"
21+
- run: git config --global user.email "[email protected]"
22+
- name: Install deps
23+
run: npm i
24+
- name: Compile code
25+
run: npm run build
26+
- name: publish package
27+
run: npx semantic-release
28+
env:
29+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
30+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
# push the version changes to GitHub
32+
- run: git add package.json && git commit -m'update version' && git push
33+
env:
34+
# The secret is passed automatically. Nothing to configure.
35+
github-token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Acceptance Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- main
8+
pull_request:
9+
branches:
10+
- '**'
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
strategy:
18+
matrix:
19+
node-version: [20.x]
20+
21+
steps:
22+
- uses: actions/checkout@v3
23+
- name: Use Node.js ${{ matrix.node-version }}
24+
uses: actions/setup-node@v3
25+
with:
26+
node-version: ${{ matrix.node-version }}
27+
- name: Run tests
28+
run: |
29+
npm i --force && npm run test

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
2-
package-lock.json
2+
package-lock.json
3+
.idea
4+
dist

jest.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
roots: ['<rootDir>'],
3+
transform: {
4+
'^.+\\.tsx?$': 'ts-jest',
5+
},
6+
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
7+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
8+
moduleDirectories: ['node_modules', '.']
9+
}

package.json

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
"name": "@codeceptjs/helper",
33
"version": "2.0.1",
44
"description": "Base class for CodeceptJS helpers",
5-
"main": "helper.js",
5+
"main": "dist/index.js",
66
"files": [
7-
"helper.js"
7+
"dist/*"
88
],
99
"scripts": {
10-
"test": "echo \"Error: no test specified\" && exit 1"
10+
"docs": "documentation readme src/index.ts -s API",
11+
"build": "tsc",
12+
"test": "jest"
1113
},
1214
"repository": {
1315
"type": "git",
@@ -16,16 +18,20 @@
1618
"keywords": [
1719
"codeceptjs"
1820
],
19-
"scripts": {
20-
"docs": "documentation readme helper.js -s API"
21-
},
2221
"author": "Michael Bodnarchuk @davert",
2322
"license": "ISC",
2423
"bugs": {
2524
"url": "https://github.com/codeceptjs/helper/issues"
2625
},
2726
"homepage": "https://github.com/codeceptjs/helper#readme",
2827
"devDependencies": {
29-
"documentation": "^13.0.2"
28+
"@types/jest": "^28.1.8",
29+
"documentation": "^13.0.2",
30+
"expect": "^28.1.3",
31+
"jest": "26.6.3",
32+
"ts-jest": "^26.5.6"
33+
},
34+
"dependencies": {
35+
"typescript": "^5.1.3"
3036
}
3137
}

helper.js renamed to src/index.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
*/
1313

1414
class Helper {
15+
private config: any;
16+
private options: any;
1517
/**
1618
*
1719
* @param {*} config
1820
*/
19-
constructor(config) {
21+
constructor(config: any) {
2022
this.config = config;
2123
}
2224

@@ -35,7 +37,7 @@ class Helper {
3537
* @returns {*}
3638
* @protected
3739
*/
38-
_validateConfig(config) {
40+
_validateConfig(config: any) {
3941
return config;
4042
}
4143

@@ -44,7 +46,7 @@ class Helper {
4446
* @param {*} opts
4547
* @protected
4648
*/
47-
_setConfig(opts) {
49+
_setConfig(opts: any) {
4850
this.options = this._validateConfig(opts);
4951
}
5052

@@ -82,7 +84,7 @@ class Helper {
8284
* @protected
8385
*/
8486
/* eslint-disable */
85-
_test(test) {
87+
_test(test: any) {
8688

8789
}
8890

@@ -92,7 +94,7 @@ class Helper {
9294
* @param {Mocha.Test} test
9395
* @protected
9496
*/
95-
_passed(test) {
97+
_passed(test: any) {
9698

9799
}
98100

@@ -102,7 +104,7 @@ class Helper {
102104
* @param {Mocha.Test} test
103105
* @protected
104106
*/
105-
_failed(test) {
107+
_failed(test: any) {
106108

107109
}
108110

@@ -112,7 +114,7 @@ class Helper {
112114
* @param {CodeceptJS.Step} step
113115
* @protected
114116
*/
115-
_beforeStep(step) {
117+
_beforeStep(step: any) {
116118

117119
}
118120

@@ -122,7 +124,7 @@ class Helper {
122124
* @param {CodeceptJS.Step} step
123125
* @protected
124126
*/
125-
_afterStep(step) {
127+
_afterStep(step: any) {
126128

127129
}
128130

@@ -132,7 +134,7 @@ class Helper {
132134
* @param {Mocha.Suite} suite
133135
* @protected
134136
*/
135-
_beforeSuite(suite) {
137+
_beforeSuite(suite: any) {
136138

137139
}
138140

@@ -142,7 +144,7 @@ class Helper {
142144
* @param {Mocha.Suite} suite
143145
* @protected
144146
*/
145-
_afterSuite(suite) {
147+
_afterSuite(suite: any) {
146148

147149
}
148150

@@ -152,15 +154,16 @@ class Helper {
152154
* @param {Mocha.Suite} suite
153155
* @protected
154156
*/
155-
_finishTest(suite) {
157+
_finishTest(suite: any) {
156158

157159
}
158160

159161
/**
160162
* Abstract method to provide common interface to accessing helpers internals inside a test.
161163
*/
162-
_useTo(description, fn) {
164+
_useTo(description: string, fn: any) {
163165
if (!description || !fn) throw new Error('useTo requires "description:string" and "fn:async function" as arguments');
166+
//@ts-ignore
164167
if (fn[Symbol.toStringTag] !== 'AsyncFunction') throw new Error(`Not async function!\n${fn}\nNative helpers API is asynchronous, please update this function be async`);
165168
fn.toString = () => 'fn()';
166169
return fn(this);
@@ -173,6 +176,7 @@ class Helper {
173176
* @type {*}
174177
*/
175178
get helpers() {
179+
// @ts-ignore
176180
const { container } = global.codeceptjs || require('codeceptjs');
177181
return container.helpers();
178182
}
@@ -182,7 +186,8 @@ class Helper {
182186
*
183187
* @param {string} msg
184188
*/
185-
debug(msg) {
189+
debug(msg: string) {
190+
// @ts-ignore
186191
const { output } = global.codeceptjs || require('codeceptjs');
187192
output.debug(msg);
188193
}
@@ -191,10 +196,11 @@ class Helper {
191196
* @param {string} section
192197
* @param {string} msg
193198
*/
194-
debugSection(section, msg) {
199+
debugSection(section: any, msg: string) {
200+
// @ts-ignore
195201
const { output } = global.codeceptjs || require('codeceptjs');
196202
output.debug(`[${section}] ${msg}`);
197203
}
198204
}
199205

200-
module.exports = Helper;
206+
export default Helper;

tests/index.spec.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import Helper from '../src';
2+
3+
let _Helper;
4+
let _CustomHelper;
5+
6+
class CustomHelper extends Helper {
7+
_validateConfig (config: any): any {
8+
super._validateConfig (config);
9+
if (!config.hello) throw Error('your config is not valid!');
10+
}
11+
}
12+
13+
describe('Abstract helper', () => {
14+
15+
beforeAll(() => {
16+
_Helper = new Helper({ hello: 'world'});
17+
})
18+
19+
test('create new helper successfully', () => {
20+
expect(_Helper.constructor.name).toEqual('Helper');
21+
});
22+
23+
test('get the passed config', () => {
24+
expect(_Helper.config).toEqual({ hello: 'world'});
25+
});
26+
27+
test('get the options from passed config', () => {
28+
_Helper._setConfig({ another: 'value' });
29+
expect(_Helper.config).toEqual({ hello: 'world'});
30+
expect(_Helper.options).toEqual({ another: 'value'});
31+
});
32+
33+
test('throws error when nothing is passed to _useTo', () => {
34+
try {
35+
_Helper._useTo();
36+
} catch (e) {
37+
expect(e.message).toContain('useTo requires "description:string" and "fn:async function" as arguments');
38+
}
39+
});
40+
41+
test('throws error when fn is not passed to _useTo', () => {
42+
try {
43+
_Helper._useTo('hello');
44+
} catch (e) {
45+
expect(e.message).toContain('useTo requires "description:string" and "fn:async function" as arguments');
46+
}
47+
});
48+
49+
test('throws error when description is not passed to _useTo', () => {
50+
try {
51+
_Helper._useTo(undefined, function () {});
52+
} catch (e) {
53+
expect(e.message).toContain('useTo requires "description:string" and "fn:async function" as arguments');
54+
}
55+
});
56+
57+
test('throws error when non async fn is passed to _useTo', () => {
58+
try {
59+
_Helper._useTo('hello', function () {});
60+
} catch (e) {
61+
expect(e.message).toContain('Not async function!');
62+
}
63+
});
64+
65+
test('no error when all valid args passed to _useTo', async () => {
66+
const res = _Helper._useTo('hello', async function hello () { return 'hi' });
67+
expect(await res).toEqual('hi');
68+
});
69+
70+
test('validate config of custom helper', async () => {
71+
try {
72+
_CustomHelper = new CustomHelper({ });
73+
_CustomHelper._validateConfig(_CustomHelper.config)
74+
} catch (e) {
75+
expect(e.message).toEqual('your config is not valid!');
76+
}
77+
});
78+
})

0 commit comments

Comments
 (0)