Skip to content

Commit

Permalink
add cjs bundle validation test
Browse files Browse the repository at this point in the history
  • Loading branch information
shuowu committed Feb 15, 2022
1 parent 663c7e0 commit afe896e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 20 deletions.
39 changes: 39 additions & 0 deletions jest.cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*!
* Copyright (c) 2015-present, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/

//Jest doc: https://jestjs.io/docs/ecmascript-modules

var OktaAuth = '<rootDir>/build/cjs/index.js';

module.exports = {
'roots': [
'test/validate-bundles'
],
'testMatch': [
'**/test/validate-bundles/**/*.{js,ts}'
],
'transform': {
'^.+\\.(ts)$': 'babel-jest'
},
'transformIgnorePatterns': [
OktaAuth
],
'restoreMocks': true,
'moduleNameMapper': {
'^@okta/okta-auth-js$': OktaAuth
},
'testPathIgnorePatterns': [],
'reporters': [
'default',
'jest-junit'
]
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"test:report": "yarn test --ci --silent || true",
"test:samples": "yarn workspace @okta/test.e2e.samples start",
"test:integration": "jest --config ./jest.integration.js",
"test:bundle:esm": "NODE_OPTIONS=--experimental-vm-modules jest --config ./jest.esm.mjs",
"test:bundle:esm": "cross-env BUNDLE_ENV=browser NODE_OPTIONS=--experimental-vm-modules jest --config ./jest.esm.mjs",
"test:bundle:cjs": "cross-env BUNDLE_ENV=node jest --config ./jest.cjs.js",
"build": "node scripts/build.js",
"build:cdn": "cross-env NODE_ENV=production webpack --config webpack.cdn.config.js",
"build:web": "cross-env NODE_ENV=production webpack --config webpack.config.js",
Expand Down
5 changes: 5 additions & 0 deletions scripts/validate-esm-bundle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ if ! yarn test:bundle:esm; then
exit ${TEST_FAILURE}
fi

if ! yarn test:bundle:cjs; then
echo "validate cjs bundle failed! Exiting..."
exit ${TEST_FAILURE}
fi

echo ${TEST_SUITE_TYPE} > ${TEST_SUITE_TYPE_FILE}
echo ${TEST_RESULT_FILE_DIR} > ${TEST_RESULT_FILE_DIR_FILE}
exit ${PUBLISH_TYPE_AND_RESULT_DIR}
72 changes: 53 additions & 19 deletions test/validate-bundles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Cookies from 'js-cookie';
import Emitter from 'tiny-emitter';
import PCancelable from 'p-cancelable';
import { OktaAuth } from '@okta/okta-auth-js';
import NodeCache from 'node-cache';

describe('OktaAuth (api)', function() {
let auth;
Expand All @@ -24,27 +25,60 @@ describe('OktaAuth (api)', function() {
expect(auth.authStateManager._pending.updateAuthStatePromise).toBeInstanceOf(PCancelable);
});

describe('js-cookie', () => {
it('get', () => {
jest.spyOn(Cookies, 'get');
auth.options.storageUtil.storage.get();
expect(Cookies.get).toHaveBeenCalled();

});
it('set', () => {
jest.spyOn(Cookies, 'set');
auth.options.storageUtil.storage.set('fakekey', 'fakevalue', '1644877195617', { secure: true, sameSite: 'none' });
expect(Cookies.set).toHaveBeenCalledWith('fakekey', 'fakevalue', {
path: '/',
sameSite: 'none',
secure: true
describe('Storage', () => {
describe('browser bundle - uses js-cookie', () => {
if (process.env.BUNDLE_ENV !== 'browser') {
return;
}

it('get', () => {
jest.spyOn(Cookies, 'get');
auth.options.storageUtil.storage.get();
expect(Cookies.get).toHaveBeenCalled();

});
it('set', () => {
jest.spyOn(Cookies, 'set');
auth.options.storageUtil.storage.set('fakekey', 'fakevalue', '1644877195617', { secure: true, sameSite: 'none' });
expect(Cookies.set).toHaveBeenCalledWith('fakekey', 'fakevalue', {
path: '/',
sameSite: 'none',
secure: true
});
});
it('delete', () => {
jest.spyOn(Cookies, 'remove');
auth.options.storageUtil.storage.delete('fakekey');
expect(Cookies.remove).toHaveBeenCalledWith('fakekey', { path: '/' });
});
});
it('remove', () => {
jest.spyOn(Cookies, 'remove');
auth.options.storageUtil.storage.delete('fakekey');
expect(Cookies.remove).toHaveBeenCalledWith('fakekey', { path: '/' });

describe('node bundle - uses node-cache', () => {
if (process.env.BUNDLE_ENV !== 'node') {
return;
}

it('use node-cache as storage', () => {
expect(auth.options.storageUtil.nodeCache).toBeInstanceOf(NodeCache);
});

it('get', () => {
jest.spyOn(auth.options.storageUtil.nodeCache, 'get');
auth.options.storageUtil.storage.get('fakekey');
expect(auth.options.storageUtil.nodeCache.get).toHaveBeenCalled();
});
it('set', () => {
jest.spyOn(auth.options.storageUtil.nodeCache, 'set');
auth.options.storageUtil.storage.set('fakekey', 'fakevalue', '1644877195617');
expect(auth.options.storageUtil.nodeCache.set).toHaveBeenCalled();
});
it('delete', () => {
jest.spyOn(auth.options.storageUtil.nodeCache, 'del');
auth.options.storageUtil.storage.delete('fakekey');
expect(auth.options.storageUtil.nodeCache.del).toHaveBeenCalled();
});
});

});

});

0 comments on commit afe896e

Please sign in to comment.