Skip to content

Commit

Permalink
Setup GitHub actions (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmandM authored May 20, 2021
1 parent 99b5066 commit 48242c2
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 33 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Node.js Package
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
- run: npm ci
- run: npm run build
- run: npm run lint
- run: npm test
publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v1
with:
node-version: '12'
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm run compile
# Publish to npm
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.PUBLISH_NPM_TOKEN }}
publish-gpr:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://npm.pkg.github.com/
scope: '@emandm'
- run: echo registry=https://npm.pkg.github.com/emandm >> .npmrc
# Publish to GitHub Packages
- run: npm ci
- run: npm run compile
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.PUBLISH_GITHUB_TOKEN }}
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
"type": "git",
"url": "git://github.com/EmandM/ts-mock-imports.git"
},
"publishConfig": {
"registry":"https://npm.pkg.github.com"
},
"keywords": [
"import",
"mock",
Expand Down
16 changes: 8 additions & 8 deletions src/import-mock.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import * as sinonModule from 'sinon';
import { MockManager, OtherManager, StaticMockManager, InPlaceMockManager } from './managers/index';
import { IConstruct, IModule, IManager } from './types';
import { InPlaceMockManager, MockManager, OtherManager, StaticMockManager } from './managers/index';
import { IConstruct, IManager, IModule } from './types';
const sinon = sinonModule as sinonModule.SinonStatic;

export class ImportMock {
private static sandboxedItems: IManager[] = [];

private static sandbox<T extends IManager>(mock: T): T {
ImportMock.sandboxedItems.push(mock);
return mock;
}

public static mockClass<T, K extends IModule = any>(
module: { [importName: string]: IConstruct<T> } | K, importName: keyof K = 'default'): MockManager<T> {
Expand Down Expand Up @@ -38,4 +32,10 @@ export class ImportMock {
ImportMock.sandboxedItems.forEach(item => item.restore());
ImportMock.sandboxedItems = [];
}
private static sandboxedItems: IManager[] = [];

private static sandbox<T extends IManager>(mock: T): T {
ImportMock.sandboxedItems.push(mock);
return mock;
}
}
38 changes: 19 additions & 19 deletions src/managers/in-place-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IModule, IManager, StringKeyOf } from '../types';
import * as sinonModule from 'sinon';
import { IManager, IModule, StringKeyOf } from '../types';
const sinon = sinonModule as sinonModule.SinonStatic;

interface IOriginal {
Expand All @@ -8,11 +8,11 @@ interface IOriginal {

export class InPlaceMockManager<T> implements IManager {
protected original: IOriginal;
protected classFunctionNames: string[]
protected classFunctionNames: string[];

constructor(protected module: IModule, protected importName: string) {
this.classFunctionNames = this.getAllFunctionNames(this.module[this.importName])
this.original = this.saveOriginal(this.classFunctionNames)
this.classFunctionNames = this.getAllFunctionNames(this.module[this.importName]);
this.original = this.saveOriginal(this.classFunctionNames);
this.mockMethods(this.classFunctionNames);
}

Expand All @@ -21,7 +21,15 @@ export class InPlaceMockManager<T> implements IManager {
}

public getMockInstance(): T {
return new this.module[this.importName];
return new this.module[this.importName]();
}

public restore() {
this.classFunctionNames.forEach((funcName) => {
if (this.original[funcName]) {
this.module[this.importName].prototype[funcName] = this.original[funcName];
}
});
}

protected mockFunction(funcName: string, returns?: any): sinon.SinonStub {
Expand Down Expand Up @@ -54,30 +62,22 @@ export class InPlaceMockManager<T> implements IManager {
// Remove duplicate methods
return funcNames;
}

protected saveOriginal(functionNames: string[]): IOriginal {
var original: IOriginal = {}
const original: IOriginal = {};
functionNames.forEach((funcName) => {
original[funcName] = this.module[this.importName].prototype[funcName]
})
return original
original[funcName] = this.module[this.importName].prototype[funcName];
});
return original;
}

protected mockMethods(functionNames: string[]) {
functionNames.forEach((funcName) => {
// Skip the constructor
if (funcName === 'constructor') {
return
return;
}
this.mock(funcName as Extract<keyof T, string>);
});
}

public restore() {
this.classFunctionNames.forEach((funcName) => {
if (this.original[funcName]) {
this.module[this.importName].prototype[funcName] = this.original[funcName]
}
});
}
}
2 changes: 1 addition & 1 deletion src/managers/manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IModule, IManager } from '../types';
import { IManager, IModule } from '../types';

export class Manager implements IManager {
protected original: any;
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './construct';
export * from './manager';
export * from './module';
export * from './stringKeyOf';
export * from './string-key-of';
2 changes: 1 addition & 1 deletion src/types/manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface IManager {
restore: () => void
restore: () => void;
}
File renamed without changes.

0 comments on commit 48242c2

Please sign in to comment.