Skip to content

Commit 0c64356

Browse files
Test refactoring and cleanup (#420)
- Clean up all of mocha CLI arguments (they were redundant) - Add a single test entrypoint (setup.js) where we load all the global dependencies - Remove all require('expect) from all files (they were not needed now) - Remove all dotEnv require (not needed anymore, initiated through setup.js) - Organize remaining tests to integration/unit folders respectfuly. - Separated some tests into their own files
1 parent cf9623c commit 0c64356

35 files changed

+136
-200
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = {
77
"extends": "airbnb-base",
88
"globals": {
99
"Atomics": "readonly",
10-
"SharedArrayBuffer": "readonly"
10+
"SharedArrayBuffer": "readonly",
11+
"expect" :"readonly"
1112
},
1213
"parserOptions": {
1314
"ecmaVersion": 2018

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"main": "cloudinary.js",
1313
"dependencies": {
1414
"cloudinary-core": "^2.10.2",
15-
"core-js": "3.6.3",
15+
"core-js": "3.6.5",
1616
"lodash": "^4.17.11",
1717
"q": "^1.5.1"
1818
},
@@ -57,7 +57,7 @@
5757
"types": "types",
5858
"scripts": {
5959
"test": "tools/scripts/test.sh",
60-
"test:unit": "mocha --ui bdd -R spec --recursive test/unit",
60+
"test:unit": "mocha --recursive test/unit",
6161
"test-with-temp-cloud": "tools/scripts/tests-with-temp-cloud.sh",
6262
"dtslint": "tools/scripts/ditslint.sh",
6363
"lint": "tools/scripts/lint.sh",

test/integration/api/admin/api_spec.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
require('dotenv').load({
2-
silent: true
3-
});
4-
5-
const expect = require("expect.js");
61
const sinon = require('sinon');
72
const ClientRequest = require('_http_client').ClientRequest;
83
const http = require('http');

test/structured_metadata_spec.js test/integration/api/admin/structured_metadata_spec.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
const expect = require("expect.js");
21
const Q = require('q');
32
const sinon = require('sinon');
4-
const cloudinary = require("../cloudinary");
5-
const helper = require("./spechelper");
6-
const TIMEOUT = require('./testUtils/testConstants').TIMEOUT;
3+
const cloudinary = require("../../../../cloudinary");
4+
const helper = require("../../../spechelper");
5+
const TIMEOUT = require('../../../testUtils/testConstants').TIMEOUT;
76

87
const TEST_ID = Date.now();
98

test/integration/api/provisioning/account_spec.js

-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
require('dotenv').load({
2-
silent: true
3-
});
4-
5-
const expect = require("expect.js");
61
const cloudinary = require("../../../../cloudinary");
72
const TIMEOUT = require('../../../testUtils/testConstants').TIMEOUT;
8-
93
let runOnlyForInternalPRs = process.env.TRAVIS_SECURE_ENV_VARS ? describe : describe.skip;
104

115

test/integration/api/search/search_spec.js

-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
require('dotenv').load({
2-
silent: true
3-
});
4-
5-
const expect = require('expect.js');
61
const Q = require('q');
72
const cloudinary = require('../../../../cloudinary');
83
const helper = require("../../../spechelper");
9-
10-
11-
124
const testConstants = require('../../../testUtils/testConstants');
135

146
const {

test/archivespec.js test/integration/api/uploader/archivespec.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
require('dotenv').load({
2-
silent: true
3-
});
4-
51
const https = require('https');
6-
const expect = require("expect.js");
72
const last = require('lodash/last');
83
const sinon = require("sinon");
94
const execSync = require('child_process').execSync;
105
const Q = require('q');
116
const fs = require('fs');
127
const os = require('os');
13-
const cloudinary = require("../cloudinary");
14-
const helper = require("./spechelper");
8+
const cloudinary = require("../../../../cloudinary");
9+
const helper = require("../../../spechelper");
1510

16-
const testConstants = require('./testUtils/testConstants');
11+
const testConstants = require('../../../testUtils/testConstants');
1712

1813
const {
1914
TIMEOUT,

test/integration/api/uploader/uploader_spec.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
require('dotenv').load({
2-
silent: true
3-
});
4-
51
const https = require('https');
62
const http = require('http');
7-
const expect = require("expect.js");
83
const sinon = require('sinon');
94
const fs = require('fs');
105
const Q = require('q');

test/cache_spec.js test/integration/cache_spec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
var options;
22
const { describe, before, it } = require('mocha');
3-
const expect = require("expect.js");
3+
44
const path = require('path');
5-
const helper = require("./spechelper");
6-
const cloudinary = require('../cloudinary').v2;
5+
const helper = require("../spechelper");
6+
const cloudinary = require('../../cloudinary').v2;
77

8-
const FileKeyValueStorage = require(`../${helper.libPath}/cache/FileKeyValueStorage`);
9-
const KeyValueCacheAdapter = require(`../${helper.libPath}/cache/KeyValueCacheAdapter`);
8+
const FileKeyValueStorage = require(`../../${helper.libPath}/cache/FileKeyValueStorage`);
9+
const KeyValueCacheAdapter = require(`../../${helper.libPath}/cache/KeyValueCacheAdapter`);
1010

1111
const Cache = cloudinary.Cache;
1212
const IMAGE_FILE = helper.IMAGE_FILE;
1313
const PUBLIC_ID = "dummy";
1414
const BREAKPOINTS = [5, 3, 7, 5];
15-
const testConstants = require('./testUtils/testConstants');
15+
const testConstants = require('../testUtils/testConstants');
1616

1717
const {
1818
TIMEOUT,

test/mocha.opts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
--require expect.js
1+
--require './test/setup.js'
22
-R spec
3-
--ui bdd
3+
--ui bdd

test/setup.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
global.expect = require('expect.js');
2+
require('./testUtils/testBootstrap');
3+
4+
5+
require('dotenv').load({
6+
silent: true
7+
});
8+
9+

test/spechelper.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const expect = require('expect.js');
21
const isFunction = require('lodash/isFunction');
32
const querystring = require('querystring');
43
const sinon = require('sinon');
@@ -7,7 +6,6 @@ const Q = require('q');
76
const http = require('http');
87
const https = require('https');
98
// Load all our custom assertions
10-
require('./testUtils/testBootstrap');
119

1210
const cloudinary = require("../cloudinary");
1311

test/streaming_profiles_spec.js

-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
require('dotenv').load({
2-
silent: true
3-
});
4-
5-
const expect = require("expect.js");
61
const keys = require('lodash/keys');
72
const Q = require('q');
83
const cloudinary = require("../cloudinary");
94
const helper = require("./spechelper");
105
const TIMEOUT = require('./testUtils/testConstants').TIMEOUT;
11-
126
const api = cloudinary.v2.api;
137

148
describe('Cloudinary::Api', function () {

test/testUtils/assertions/beADatasource.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const expect = require('expect.js');
21
const isEmpty = require("lodash/isEmpty");
32
const includes = require('lodash/includes');
43

test/testUtils/assertions/beAMetadataField.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const expect = require('expect.js');
21
const includes = require('lodash/includes');
32

43
/**

test/testUtils/assertions/beServedByCloudinary.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const expect = require('expect.js');
21
const cloneDeep = require('lodash/cloneDeep');
32
const http = require('http');
43
const https = require('https');

test/testUtils/assertions/emptyOptions.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const expect = require('expect.js');
21
const isEmpty = require("lodash/isEmpty");
32
const cloneDeep = require('lodash/cloneDeep');
43
const utils = require('../../../cloudinary').utils;

test/testUtils/assertions/produceUrl.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const expect = require('expect.js');
21
const cloneDeep = require('lodash/cloneDeep');
32
const cloudinary = require('../../../cloudinary');
43

test/access_control_spec.js test/unit/access_control_spec.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
require('dotenv').load({
2-
silent: true
3-
});
4-
5-
const expect = require("expect.js");
61
const isString = require('lodash/isString');
7-
const cloudinary = require("../cloudinary");
8-
2+
const cloudinary = require("../../cloudinary");
93
const build_upload_params = cloudinary.utils.build_upload_params;
104

115
const ACL = {

test/unit/authToken/authTokenImageTag_spec.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
require('dotenv').load({
2-
silent: true
3-
});
4-
5-
const expect = require("expect.js");
61
const cloudinary = require("../../../cloudinary.js");
72
const commonAuthSetupAndTeardown = require('./utils/commonAuthSetupAndTeardown');
83

test/unit/authToken/authTokenURL_spec.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
require('dotenv').load({
2-
silent: true
3-
});
4-
5-
const expect = require("expect.js");
61
const cloudinary = require("../../../cloudinary.js");
72
const commonAuthSetupAndTeardown = require('./utils/commonAuthSetupAndTeardown');
83

test/unit/authToken/authTokenUtils_spec.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
require('dotenv').load({
2-
silent: true
3-
});
4-
5-
const expect = require("expect.js");
61
const cloudinary = require("../../../cloudinary.js");
72
const commonAuthSetupAndTeardown = require('./utils/commonAuthSetupAndTeardown');
83

test/unit/cache/FileKeyValueCache_spec.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const expect = require("expect.js");
21
const fs = require('fs');
32
const path = require('path');
43
const helper = require("../../spechelper");

test/unit/cache/KeyValueCacheAdapter_spec.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
const expect = require("expect.js");
21
const helper = require("../../spechelper");
3-
42
const DummyCacheStorage = require('./DummyCacheStorage');
5-
63
const KeyValueCacheAdapter = require(`../../../${helper.libPath}/cache/KeyValueCacheAdapter`);
74

85
var cache;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
const cloudinary = require("../../../cloudinary.js");
3+
4+
describe("getUserAgent", function () {
5+
var platform = "";
6+
before(function () {
7+
platform = cloudinary.utils.userPlatform;
8+
cloudinary.utils.userPlatform = "";
9+
});
10+
after(function () {
11+
cloudinary.utils.userPlatform = platform;
12+
});
13+
it("should add a user platform to USER_AGENT", function () {
14+
cloudinary.utils.userPlatform = "Spec/1.0 (Test)";
15+
expect(cloudinary.utils.getUserAgent()).to.match(/Spec\/1.0 \(Test\) CloudinaryNodeJS\/[\d.]+/);
16+
});
17+
});

test/cloudinary_spec.js test/unit/cloudinary_spec.js

+2-91
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const expect = require("expect.js");
2-
const cloudinary = require("../cloudinary.js");
3-
const createTestConfig = require('./testUtils/createTestConfig');
1+
const cloudinary = require("../../cloudinary.js");
2+
const createTestConfig = require('../testUtils/createTestConfig');
43

54
describe("cloudinary", function () {
65
beforeEach(function () {
@@ -793,92 +792,4 @@ describe("cloudinary", function () {
793792
result = cloudinary.utils.url("sample.jpg", options);
794793
expect(result).to.eql('http://res.cloudinary.com/test123/image/upload/s--v2fTPYTu--/sample.jpg');
795794
});
796-
describe("getUserAgent", function () {
797-
var platform = "";
798-
before(function () {
799-
platform = cloudinary.utils.userPlatform;
800-
cloudinary.utils.userPlatform = "";
801-
});
802-
after(function () {
803-
cloudinary.utils.userPlatform = platform;
804-
});
805-
it("should add a user platform to USER_AGENT", function () {
806-
cloudinary.utils.userPlatform = "Spec/1.0 (Test)";
807-
expect(cloudinary.utils.getUserAgent()).to.match(/Spec\/1.0 \(Test\) CloudinaryNodeJS\/[\d.]+/);
808-
});
809-
});
810-
describe("config", function () {
811-
let cloudinaryUrlBackup;
812-
let accountUrlBackup;
813-
before(function () {
814-
cloudinaryUrlBackup = process.env.CLOUDINARY_URL;
815-
accountUrlBackup = process.env.CLOUDINARY_ACCOUNT_URL;
816-
});
817-
describe("CLOUDINARY_URL", function () {
818-
after(function () {
819-
process.env.CLOUDINARY_URL = cloudinaryUrlBackup || '';
820-
cloudinary.config(true);
821-
});
822-
it("should allow nested values in CLOUDINARY_URL", function () {
823-
process.env.CLOUDINARY_URL = "cloudinary://key:secret@test123?foo[bar]=value";
824-
cloudinary.config(true);
825-
const foo = cloudinary.config().foo;
826-
expect(foo && foo.bar).to.eql('value');
827-
});
828-
it("should load a properly formatted CLOUDINARY_URL", function () {
829-
process.env.CLOUDINARY_URL = "cloudinary://123456789012345:ALKJdjklLJAjhkKJ45hBK92baj3@test";
830-
cloudinary.config(true);
831-
});
832-
it("should not be sensitive to case in CLOUDINARY_URL's protocol", function () {
833-
process.env.CLOUDINARY_URL = "CLouDiNaRY://123456789012345:ALKJdjklLJAjhkKJ45hBK92baj3@test";
834-
cloudinary.config(true);
835-
});
836-
it("should throw error when CLOUDINARY_URL doesn't start with 'cloudinary://'", function () {
837-
process.env.CLOUDINARY_URL = "https://123456789012345:ALKJdjklLJAjhkKJ45hBK92baj3@test?cloudinary=foo";
838-
try {
839-
cloudinary.config(true);
840-
expect().fail();
841-
} catch (err) {
842-
expect(err.message).to.eql("Invalid CLOUDINARY_URL protocol. URL should begin with 'cloudinary://'");
843-
}
844-
});
845-
it("should not throw an error when CLOUDINARY_URL environment variable is missing", function () {
846-
delete process.env.CLOUDINARY_URL;
847-
cloudinary.config(true);
848-
});
849-
});
850-
describe("CLOUDINARY_ACCOUNT_URL", function () {
851-
after(function () {
852-
process.env.CLOUDINARY_ACCOUNT_URL = accountUrlBackup || '';
853-
cloudinary.config(true);
854-
});
855-
it("should allow nested values in CLOUDINARY_ACCOUNT_URL", function () {
856-
process.env.CLOUDINARY_ACCOUNT_URL = "account://key:secret@test123?foo[bar]=value";
857-
cloudinary.config(true);
858-
const foo = cloudinary.config().foo;
859-
expect(foo && foo.bar).to.eql('value');
860-
});
861-
it("should load a properly formatted CLOUDINARY_ACCOUNT_URL", function () {
862-
process.env.CLOUDINARY_ACCOUNT_URL = "account://635412789012345:ALKJdjklLJAjhkKJ45hBK92tam2@test1";
863-
cloudinary.config(true);
864-
});
865-
it("should not be sensitive to case in CLOUDINARY_ACCOUNT_URL's protocol", function () {
866-
process.env.CLOUDINARY_ACCOUNT_URL = "aCCouNT://635283989012345:ALKGssklLJAjhkKJ45hBK92tas5@test1";
867-
cloudinary.config(true);
868-
});
869-
it("should throw error when CLOUDINARY_ACCOUNT_URL doesn't start with 'account://'", function () {
870-
process.env.CLOUDINARY_ACCOUNT_URL = "https://635283989012345:ALKGssklLJAjhkKJ45hBK92tas5@test1?account=foo";
871-
try {
872-
cloudinary.config(true);
873-
expect().fail();
874-
} catch (err) {
875-
expect(err.message).to.eql("Invalid CLOUDINARY_ACCOUNT_URL protocol. URL should begin with 'account://'");
876-
}
877-
});
878-
it("should not throw an error when CLOUDINARY_ACCOUNT_URL environment variable is missing", function () {
879-
delete process.env.CLOUDINARY_ACCOUNT_URL;
880-
cloudinary.config(true);
881-
});
882-
});
883-
});
884795
});

0 commit comments

Comments
 (0)