Skip to content

Commit

Permalink
feat(byods): Spark 549034 jest setup (#3821)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesari3008 authored Sep 12, 2024
1 parent 97d8936 commit 0c1a758
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 6 deletions.
47 changes: 47 additions & 0 deletions packages/byods/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import config from '@webex/jest-config-legacy';

const jestConfig = {
rootDir: './',
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
testEnvironment: 'node',
testMatch: ['<rootDir>/**/*.test.ts'],
transformIgnorePatterns: ['/node_modules/(?!node-fetch)|data-uri-to-buffer'],
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
testResultsProcessor: 'jest-junit',
// Clear mocks in between tests by default
clearMocks: true,
// TODO: Set this to true once we have the source code and their corresponding test files added
collectCoverage: false,
coverageThreshold: {
global: {
lines: 85,
functions: 85,
branches: 85,
statements: 85,
},
},
coverageDirectory: 'coverage',
coverageReporters: ['clover', 'json', 'lcov'],
reporters: [
'default',
[
'jest-junit',
{
outputDirectory: 'coverage/junit',
outputName: 'coverage-junit.xml',
classNameTemplate: '{classname}',
titleTemplate: '{title}',
},
],
[
'jest-html-reporters',
{
publicPath: './coverage',
filename: 'jest-report.html',
openReport: false,
},
],
],
};

export default {...config, ...jestConfig};
10 changes: 10 additions & 0 deletions packages/byods/jest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'jest';

declare global {
namespace jest {
/* eslint-disable-next-line no-unused-vars */
interface Matchers<R> {
toBeCalledOnceWith(received?: any, ...expected: any[]): CustomMatcherResult;
}
}
}
12 changes: 12 additions & 0 deletions packages/byods/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
expect.extend({
toBeCalledOnceWith(received, ...expected) {
try {
expect(received).toBeCalledTimes(1);
expect(received).toBeCalledWith(...expected);
} catch (error) {
return {message: () => error, pass: false};
}

return {pass: true};
},
});
6 changes: 5 additions & 1 deletion packages/byods/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
"docs": "typedoc --emit none",
"fix:lint": "eslint 'src/**/*.ts' --fix",
"fix:prettier": "prettier \"src/**/*.ts\" --write",
"prebuild": "rimraf dist"
"prebuild": "rimraf dist",
"test": "yarn test:style && yarn test:unit",
"test:style": "eslint 'src/**/*.ts'",
"test:unit": "webex-legacy-tools test --unit --runner jest"
},
"devDependencies": {
"@babel/preset-typescript": "7.16.7",
Expand Down Expand Up @@ -112,6 +115,7 @@
},
"dependencies": {
"@types/node-fetch": "^2.6.11",
"@webex/jest-config-legacy": "workspace:*",
"@webex/legacy-tools": "workspace:*",
"@webex/media-helpers": "workspace:*",
"node-fetch": "^3.3.2"
Expand Down
3 changes: 1 addition & 2 deletions packages/byods/src/BYODS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-console */
import fetch from 'node-fetch';
import {LocalStream} from '@webex/media-helpers'; // Just to show that you can import other packages from the workspace

interface SDKConfig {
clientId: string;
Expand Down Expand Up @@ -44,4 +43,4 @@ class BYODS {
}

export default BYODS;
export {SDKConfig, LocalStream};
export {SDKConfig};
32 changes: 32 additions & 0 deletions packages/byods/src/BYoDS.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fetch, {Response} from 'node-fetch';
import BYODS from './BYODS';

jest.mock('node-fetch', () => jest.fn());

describe('BYoDS Tests', () => {
const mockSDKConfig = {
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
accessToken: 'your-initial-access-token',
refreshToken: 'your-refresh-token',
expiresAt: new Date('2024-09-15T00:00:00Z'),
};

const mockResponse = {
json: jest.fn().mockResolvedValue({key: 'value'}),
} as unknown as Response;

(fetch as unknown as jest.MockedFunction<typeof fetch>).mockResolvedValue(mockResponse);

it('fetch the datasources', async () => {
const mockPayload = {
headers: {
Authorization: `Bearer ${mockSDKConfig.accessToken}`,
},
};
const sdk = new BYODS(mockSDKConfig);
const endpoint = 'https://developer-applications.ciscospark.com/v1/dataSources/';
await sdk.makeAuthenticatedRequest(endpoint);
expect(fetch).toHaveBeenCalledWith(endpoint, mockPayload);
});
});
2 changes: 2 additions & 0 deletions packages/legacy/tools/src/models/package/package.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const PATTERNS = {
JAVASCRIPT: './**/*.js',
TYPESCRIPT: './**/*.ts',
TEST: './**/*.*',
BYODS: './*.test.ts',
};

/**
Expand All @@ -14,6 +15,7 @@ const TEST_DIRECTORIES = {
INTEGRATION: './integration/spec',
ROOT: './test',
UNIT: './unit/spec',
SRC: './src',
};

const CONSTANTS = {
Expand Down
14 changes: 11 additions & 3 deletions packages/legacy/tools/src/models/package/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ class Package {
public test(config: TestConfig): Promise<this> {
const testDirectory = path.join(this.data.packageRoot, CONSTANTS.TEST_DIRECTORIES.ROOT);

const unitTestFileCollectorInSrc = config.unit
? Package.getFiles({
location: path.join(this.data.packageRoot, CONSTANTS.TEST_DIRECTORIES.SRC),
pattern: CONSTANTS.PATTERNS.BYODS,
targets: config.targets,
})
: Promise.resolve([]);

const unitTestFileCollector = config.unit
? Package.getFiles({
location: path.join(testDirectory, CONSTANTS.TEST_DIRECTORIES.UNIT),
Expand All @@ -116,10 +124,10 @@ class Package {
})
: Promise.resolve([]);

return Promise.all([unitTestFileCollector, integrationTestFileCollector])
.then(async ([unitFiles, integrationFiles]) => {
return Promise.all([unitTestFileCollector, integrationTestFileCollector, unitTestFileCollectorInSrc])
.then(async ([unitFiles, integrationFiles, srcUnitFiles]) => {
if (config.runner === 'jest') {
const testFiles = [...unitFiles];
const testFiles = [...unitFiles, ...srcUnitFiles];

if (testFiles.length > 0) {
await Jest.test({ files: testFiles });
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7429,6 +7429,7 @@ __metadata:
"@typescript-eslint/eslint-plugin": 5.38.1
"@typescript-eslint/parser": 5.38.1
"@web/dev-server": 0.4.5
"@webex/jest-config-legacy": "workspace:*"
"@webex/legacy-tools": "workspace:*"
"@webex/media-helpers": "workspace:*"
chai: 4.3.4
Expand Down

0 comments on commit 0c1a758

Please sign in to comment.