forked from axios/axios
/
test.js
183 lines (139 loc) · 4.53 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import assert from 'assert';
import * as axios from '../../index.js';
import axiosFactory from '../../lib/axios.js';
import utils from "../../lib/utils.js";
import {fileURLToPath} from 'url';
import path from 'path';
import util from "util";
import cp from "child_process";
import fs from 'fs-extra';
const BACKUP_PATH = './backup/';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const exec = util.promisify(cp.exec);
const spawn = (command, args) => new Promise((resolve, reject) => {
cp.spawn(command, args, {
shell : true,
stdio: 'inherit'
}).once('error', reject).on(
'close',
(code) => code ? reject(new Error(`Exit code ${code}`)) : resolve()
);
});
const {Axios} = axiosFactory;
const ignoreList = ['default'];
const instance = axiosFactory.create({});
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const remove = async (file) => {
console.log(`✓ Remove entry '${file}'...`);
try {
await sleep(100);
await fs.remove(file);
} catch (err) {
console.warn(err.message);
}
}
describe('module', function () {
before(async ()=> {
console.log('✓ Creating build backup...');
await fs.copy('./dist/', BACKUP_PATH);
console.log('✓ Exec build script...');
await exec('npm run build');
console.log('✓ Running tests...');
});
after(async () => {
console.log('✓ Restore build from the backup...');
await fs.copy(BACKUP_PATH, './dist/');
await remove(BACKUP_PATH);
});
it('should have consistent ESM export', function () {
const namedExport = {};
const factoryExport = {};
Object.entries(axiosFactory).forEach(([key, value]) => {
if(!utils.hasOwnProp(Axios, key) && !(key in instance) && ignoreList.indexOf(key) === -1) {
factoryExport[key] = value;
}
});
Object.entries(axios).forEach(([key, value]) => {
key!=='default' && ignoreList.indexOf(key) === -1 && (namedExport[key] = value);
});
assert.deepStrictEqual(namedExport, factoryExport);
});
describe('CommonJS', ()=> {
const pkgPath = path.join(__dirname, './cjs');
after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
});
it('should be able to be loaded with require', async function () {
this.timeout(30000);
await exec(`npm test --prefix ${pkgPath}`);
});
});
describe('ESM', ()=> {
const pkgPath = path.join(__dirname, './esm');
after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
});
it('should be able to be loaded with import', async function () {
this.timeout(30000);
await exec(`npm test --prefix ${pkgPath}`);
});
});
describe('TS', ()=> {
const pkgPath = path.join(__dirname, './ts');
after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
});
it('should be able to be loaded with import', async function () {
this.timeout(30000);
await exec(`npm test --prefix ${pkgPath}`, {});
});
});
describe('TS require(\'axios\')', ()=> {
const pkgPath = path.join(__dirname, './ts-require');
after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
});
it('should be able to be loaded with require', async function () {
this.timeout(30000);
await exec(`npm test --prefix ${pkgPath}`, {});
});
});
describe('TS require(\'axios\').default', ()=> {
const pkgPath = path.join(__dirname, './ts-require-default');
after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
});
it('should be able to be loaded with require', async function () {
this.timeout(30000);
await exec(`npm test --prefix ${pkgPath}`, {});
});
});
describe('typings', () => {
describe('ESM', ()=> {
const pkgPath = path.join(__dirname, './typings/esm');
after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
});
it('should pass types check', async function () {
this.timeout(30000);
await spawn(`npm test --prefix ${pkgPath}`, [], {
shell : true,
stdio: 'pipe'
});
});
});
describe('CommonJS', ()=> {
const pkgPath = path.join(__dirname, './typings/cjs');
after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
});
it('should pass types check', async function () {
this.timeout(30000);
await spawn(`npm test --prefix ${pkgPath}`, [], {
shell : true,
stdio: 'pipe'
});
});
});
});
});