Skip to content

Commit

Permalink
Merge pull request #6 from codeceptjs/move-to-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent authored Feb 10, 2024
2 parents 12acde4 + 9b3846e commit f9a4595
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 50 deletions.
56 changes: 29 additions & 27 deletions .github/workflows/publish-node.js.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
# 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
# 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
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Publish
name: Publish npm Package

on:
release:
types: [published]
jobs:
publish:
push:
branches:
- master
- main

jobs:
publish-npm:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.release.target_commitish }}
- uses: actions/setup-node@v2
with:
node-version: 16
cache: 'npm'
- run: git config --global user.name "GitHub CD bot"
- run: git config --global user.email "github-cd-bot@example.com"
- run: npm version ${{ github.event.release.tag_name }}
- uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.NPM_TOKEN }}

# push the version changes to GitHub
- run: git push
env:
# The secret is passed automatically. Nothing to configure.
github-token: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- run: git config --global user.name "GitHub CD bot"
- run: git config --global user.email "github-cd-bot@example.com"
- name: Install deps
run: npm i
- name: Compile code
run: npm run build
- name: publish package
run: npx semantic-release
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# push the version changes to GitHub
- run: git add package.json && git commit -m'update version' && git push
env:
# The secret is passed automatically. Nothing to configure.
github-token: ${{ secrets.GITHUB_TOKEN }}
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Acceptance Tests

on:
push:
branches:
- master
- main
pull_request:
branches:
- '**'

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Run tests
run: |
npm i --force && npm run test
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
package-lock.json
package-lock.json
.idea
dist
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
roots: ['<rootDir>'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleDirectories: ['node_modules', '.']
}
20 changes: 13 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"name": "@codeceptjs/helper",
"version": "2.0.1",
"description": "Base class for CodeceptJS helpers",
"main": "helper.js",
"main": "dist/index.js",
"files": [
"helper.js"
"dist/*"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"docs": "documentation readme src/index.ts -s API",
"build": "tsc",
"test": "jest"
},
"repository": {
"type": "git",
Expand All @@ -16,16 +18,20 @@
"keywords": [
"codeceptjs"
],
"scripts": {
"docs": "documentation readme helper.js -s API"
},
"author": "Michael Bodnarchuk @davert",
"license": "ISC",
"bugs": {
"url": "https://github.com/codeceptjs/helper/issues"
},
"homepage": "https://github.com/codeceptjs/helper#readme",
"devDependencies": {
"documentation": "^13.0.2"
"@types/jest": "^28.1.8",
"documentation": "^13.0.2",
"expect": "^28.1.3",
"jest": "26.6.3",
"ts-jest": "^26.5.6"
},
"dependencies": {
"typescript": "^5.1.3"
}
}
36 changes: 21 additions & 15 deletions helper.js → src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
*/

class Helper {
private config: any;
private options: any;
/**
*
* @param {*} config
*/
constructor(config) {
constructor(config: any) {
this.config = config;
}

Expand All @@ -35,7 +37,7 @@ class Helper {
* @returns {*}
* @protected
*/
_validateConfig(config) {
_validateConfig(config: any) {
return config;
}

Expand All @@ -44,7 +46,7 @@ class Helper {
* @param {*} opts
* @protected
*/
_setConfig(opts) {
_setConfig(opts: any) {
this.options = this._validateConfig(opts);
}

Expand Down Expand Up @@ -82,7 +84,7 @@ class Helper {
* @protected
*/
/* eslint-disable */
_test(test) {
_test(test: any) {

}

Expand All @@ -92,7 +94,7 @@ class Helper {
* @param {Mocha.Test} test
* @protected
*/
_passed(test) {
_passed(test: any) {

}

Expand All @@ -102,7 +104,7 @@ class Helper {
* @param {Mocha.Test} test
* @protected
*/
_failed(test) {
_failed(test: any) {

}

Expand All @@ -112,7 +114,7 @@ class Helper {
* @param {CodeceptJS.Step} step
* @protected
*/
_beforeStep(step) {
_beforeStep(step: any) {

}

Expand All @@ -122,7 +124,7 @@ class Helper {
* @param {CodeceptJS.Step} step
* @protected
*/
_afterStep(step) {
_afterStep(step: any) {

}

Expand All @@ -132,7 +134,7 @@ class Helper {
* @param {Mocha.Suite} suite
* @protected
*/
_beforeSuite(suite) {
_beforeSuite(suite: any) {

}

Expand All @@ -142,7 +144,7 @@ class Helper {
* @param {Mocha.Suite} suite
* @protected
*/
_afterSuite(suite) {
_afterSuite(suite: any) {

}

Expand All @@ -152,15 +154,16 @@ class Helper {
* @param {Mocha.Suite} suite
* @protected
*/
_finishTest(suite) {
_finishTest(suite: any) {

}

/**
* Abstract method to provide common interface to accessing helpers internals inside a test.
*/
_useTo(description, fn) {
_useTo(description: string, fn: any) {
if (!description || !fn) throw new Error('useTo requires "description:string" and "fn:async function" as arguments');
//@ts-ignore
if (fn[Symbol.toStringTag] !== 'AsyncFunction') throw new Error(`Not async function!\n${fn}\nNative helpers API is asynchronous, please update this function be async`);
fn.toString = () => 'fn()';
return fn(this);
Expand All @@ -173,6 +176,7 @@ class Helper {
* @type {*}
*/
get helpers() {
// @ts-ignore
const { container } = global.codeceptjs || require('codeceptjs');
return container.helpers();
}
Expand All @@ -182,7 +186,8 @@ class Helper {
*
* @param {string} msg
*/
debug(msg) {
debug(msg: string) {
// @ts-ignore
const { output } = global.codeceptjs || require('codeceptjs');
output.debug(msg);
}
Expand All @@ -191,10 +196,11 @@ class Helper {
* @param {string} section
* @param {string} msg
*/
debugSection(section, msg) {
debugSection(section: any, msg: string) {
// @ts-ignore
const { output } = global.codeceptjs || require('codeceptjs');
output.debug(`[${section}] ${msg}`);
}
}

module.exports = Helper;
export default Helper;
78 changes: 78 additions & 0 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import Helper from '../src';

let _Helper;
let _CustomHelper;

class CustomHelper extends Helper {
_validateConfig (config: any): any {
super._validateConfig (config);
if (!config.hello) throw Error('your config is not valid!');
}
}

describe('Abstract helper', () => {

beforeAll(() => {
_Helper = new Helper({ hello: 'world'});
})

test('create new helper successfully', () => {
expect(_Helper.constructor.name).toEqual('Helper');
});

test('get the passed config', () => {
expect(_Helper.config).toEqual({ hello: 'world'});
});

test('get the options from passed config', () => {
_Helper._setConfig({ another: 'value' });
expect(_Helper.config).toEqual({ hello: 'world'});
expect(_Helper.options).toEqual({ another: 'value'});
});

test('throws error when nothing is passed to _useTo', () => {
try {
_Helper._useTo();
} catch (e) {
expect(e.message).toContain('useTo requires "description:string" and "fn:async function" as arguments');
}
});

test('throws error when fn is not passed to _useTo', () => {
try {
_Helper._useTo('hello');
} catch (e) {
expect(e.message).toContain('useTo requires "description:string" and "fn:async function" as arguments');
}
});

test('throws error when description is not passed to _useTo', () => {
try {
_Helper._useTo(undefined, function () {});
} catch (e) {
expect(e.message).toContain('useTo requires "description:string" and "fn:async function" as arguments');
}
});

test('throws error when non async fn is passed to _useTo', () => {
try {
_Helper._useTo('hello', function () {});
} catch (e) {
expect(e.message).toContain('Not async function!');
}
});

test('no error when all valid args passed to _useTo', async () => {
const res = _Helper._useTo('hello', async function hello () { return 'hi' });
expect(await res).toEqual('hi');
});

test('validate config of custom helper', async () => {
try {
_CustomHelper = new CustomHelper({ });
_CustomHelper._validateConfig(_CustomHelper.config)
} catch (e) {
expect(e.message).toEqual('your config is not valid!');
}
});
})
Loading

0 comments on commit f9a4595

Please sign in to comment.