chore(): add test cases and adjust ci

This commit is contained in:
Shariar Shaikot
2021-10-23 00:06:32 +02:00
parent 88c7d759d5
commit 1218c5ab11
7 changed files with 6308 additions and 5 deletions

View File

@@ -1,5 +1,7 @@
language: node_js
node_js:
- "5"
- "12.14.1"
script:
- npm run lint
- npm test
- npm run build

36
index.spec.ts Normal file
View File

@@ -0,0 +1,36 @@
import { TextDecoder } from 'util'
import { default as WebVTT, moduleName } from './index'
describe('WebVTT', () => {
let file: Blob;
beforeEach(() => {
file = new Blob([new Uint8Array([])], { type: 'text/plain' });
(global as any).TextDecoder = TextDecoder;
(global.URL as any).createObjectURL = jest.fn().mockImplementation((text) => 'blob:some-valid-url');
})
it('should return a promise', () => {
expect(WebVTT(file)).toBeInstanceOf(Promise);
});
it('should return a valid return', async () => {
expect(await WebVTT(file)).toBe('blob:some-valid-url');
});
it('should throw if undefined or wrong resource provided', async () => {
try {
const webVtt = await WebVTT(undefined as any);
} catch (e: any) {
expect(e.message).toContain(moduleName);
expect(e.message).toContain('Blob');
}
});
it('should throw TextDecoder is not defined', async () => {
(global as any).TextDecoder = undefined;
try {
const webVtt = await WebVTT(file);
} catch (e: any) {
expect(e.message).toContain(moduleName);
expect(e.message).toContain('TextDecoder');
}
});
});

View File

@@ -1,4 +1,4 @@
const moduleName = 'toWebVTT';
export const moduleName = 'toWebVTT';
const blobToBufferOrString = (blob: Blob, readAs: 'string' | 'buffer'): Promise<Uint8Array | String> =>
new Promise((resolve, reject) => {
@@ -42,7 +42,7 @@ const toWebVTT = async (resource: Blob): Promise<string> => {
throw (new Error(`${moduleName}: No TextDecoder constructor found`));
}
if (!(resource instanceof Blob)) {
throw (new Error(`${moduleName}: Expecting resource to be a Blob but something else found.`));
throw new Error(`${moduleName}: Expecting resource to be a Blob but something else found.`);
}
let text;
const vttString = 'WEBVTT FILE\r\n\r\n';

4
jest.config.js Normal file
View File

@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
};

6257
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -8,7 +8,8 @@
"lib/index.d.ts"
],
"scripts": {
"test": "./node_modules/.bin/eslint ./index.js",
"test": "./node_modules/.bin/jest",
"lint": "./node_modules/.bin/eslint ./index.js",
"build": "rm -rf lib && tsc",
"minor": "npm version minor && npm publish",
"major": "npm version major && npm publish",
@@ -45,11 +46,14 @@
},
"homepage": "https://github.com/imshaikot/srt-webvtt#readme",
"devDependencies": {
"@types/jest": "^27.0.2",
"eslint": "^4.15.0",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.5.1",
"jest": "^27.3.1",
"ts-jest": "^27.0.7",
"typescript": "^4.4.4"
}
}

View File

@@ -10,6 +10,6 @@
},
"exclude": [
"node_modules",
"dist"
"lib"
]
}