Skip to content

Commit

Permalink
Refactored Karma tests to use rollup preprocessor & ESM;
Browse files Browse the repository at this point in the history
Replaced grunt with gulp;
Improved dev scripts;
  • Loading branch information
DigitalBrainJS committed Jun 14, 2022
1 parent be2ad1d commit 462ed4c
Show file tree
Hide file tree
Showing 66 changed files with 5,364 additions and 6,590 deletions.
File renamed without changes.
93 changes: 0 additions & 93 deletions Gruntfile.js

This file was deleted.

2 changes: 1 addition & 1 deletion bin/ssl_hotfix.js
@@ -1,4 +1,4 @@
const {spawn} = require('child_process');
import {spawn} from 'child_process';

const args = process.argv.slice(2);

Expand Down
4 changes: 2 additions & 2 deletions examples/get/server.js
@@ -1,4 +1,4 @@
var people = [
const people = [
{
"name": "Matt Zabriskie",
"github": "mzabriskie",
Expand All @@ -25,7 +25,7 @@ var people = [
}
];

module.exports = function (req, res) {
export default function (req, res) {
res.writeHead(200, {
'Content-Type': 'text/json'
});
Expand Down
4 changes: 2 additions & 2 deletions examples/post/server.js
@@ -1,5 +1,5 @@
module.exports = function (req, res) {
var data = '';
export default function (req, res) {
let data = '';

req.on('data', function (chunk) {
data += chunk;
Expand Down
2 changes: 1 addition & 1 deletion examples/postMultipartFormData/server.js
@@ -1,4 +1,4 @@
module.exports = function (req, res) {
export default function (req, res) {

req.on('data', function (chunk) {
});
Expand Down
37 changes: 21 additions & 16 deletions examples/server.js
@@ -1,18 +1,23 @@
var fs = require('fs');
var path = require('path');
var http = require('http');
var argv = require('minimist')(process.argv.slice(2));
var server;
var dirs;
import fs from 'fs';
import path from 'path';
import http from 'http';
import minimist from 'minimist';
import url from "url";
const argv = minimist(process.argv.slice(2));
let server;
let dirs;

const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

function listDirs(root) {
var files = fs.readdirSync(root);
var dirs = [];
const files = fs.readdirSync(root);
const dirs = [];

for (var i = 0, l = files.length; i < l; i++) {
var file = files[i];
for (let i = 0, l = files.length; i < l; i++) {
const file = files[i];
if (file[0] !== '.') {
var stat = fs.statSync(path.join(root, file));
const stat = fs.statSync(path.join(root, file));
if (stat.isDirectory()) {
dirs.push(file);
}
Expand All @@ -23,8 +28,8 @@ function listDirs(root) {
}

function getIndexTemplate() {
var links = dirs.map(function (dir) {
var url = '/' + dir;
const links = dirs.map(function (dir) {
const url = '/' + dir;
return '<li onclick="document.location=\'' + url + '\'"><a href="' + url + '">' + url + '</a></li>';
});

Expand Down Expand Up @@ -74,7 +79,7 @@ function pipeFileToResponse(res, file, type) {
dirs = listDirs(__dirname);

server = http.createServer(function (req, res) {
var url = req.url;
let url = req.url;

// Process axios itself
if (/axios\.min\.js$/.test(url)) {
Expand Down Expand Up @@ -106,7 +111,7 @@ server = http.createServer(function (req, res) {
}

// Format request /get -> /get/index.html
var parts = url.split('/');
const parts = url.split('/');
if (dirs.indexOf(parts[parts.length - 1]) > -1) {
url += '/index.html';
}
Expand Down Expand Up @@ -136,5 +141,5 @@ server = http.createServer(function (req, res) {
const PORT = argv.p || 3000;

server.listen(PORT, () => {
console.log(`Examples running on ${PORT}`);
console.log(`Examples running on ${PORT}`);
});
4 changes: 2 additions & 2 deletions examples/upload/server.js
@@ -1,5 +1,5 @@
module.exports = function (req, res) {
var data = '';
export default function (req, res) {
let data = '';

req.on('data', function (chunk) {
data += chunk;
Expand Down
52 changes: 52 additions & 0 deletions gulpfile.js
@@ -0,0 +1,52 @@
import gulp from 'gulp';
import fs from 'fs-extra';

gulp.task('default', async function(){
console.log('hello!');
});

const clear = gulp.task('clear', async function() {
await fs.emptyDir('./dist/')
});

const bower = gulp.task('bower', async function () {
const [npm, bower] = await Promise.all([
fs.readFileSync('package.json'),
fs.readFileSync('bower.json')
]);

const fields = [
'name',
'description',
'version',
'homepage',
'license',
'keywords'
];

for (let i = 0, l = fields.length; i < l; i++) {
const field = fields[i];
bower[field] = npm[field];
}

await fs.writeFile('bower.json', JSON.stringify(bower, null, 2));
});

const env = gulp.task('env', async function () {
var npm = JSON.parse(await fs.readFile('package.json'));

await fs.writeFile('./lib/env/data.js', Object.entries({
VERSION: npm.version
}).map(([key, value]) => {
return `export const ${key} = ${JSON.stringify(value)};`
}).join('\n'));
});

const version = gulp.parallel(bower, env);

export {
bower,
env,
clear,
version
}
23 changes: 18 additions & 5 deletions karma.conf.js → karma.conf.cjs
Expand Up @@ -6,7 +6,8 @@

'use strict';

var webpack = require('webpack');
var resolve = require('@rollup/plugin-node-resolve').default;
var commonjs = require('@rollup/plugin-commonjs');

function createCustomLauncher(browser, version, platform) {
return {
Expand Down Expand Up @@ -147,8 +148,8 @@ module.exports = function(config) {

// list of files / patterns to load in the browser
files: [
'test/specs/__helpers.js',
'test/specs/**/*.spec.js'
{pattern: 'test/specs/__helpers.js', watched: false},
{pattern: 'test/specs/**/*.spec.js', watched: false}
],


Expand All @@ -159,8 +160,20 @@ module.exports = function(config) {
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test/specs/__helpers.js': ['webpack', 'sourcemap'],
'test/specs/**/*.spec.js': ['webpack', 'sourcemap']
'test/specs/__helpers.js': ['rollup'],
'test/specs/**/*.spec.js': ['rollup']
},

rollupPreprocessor: {
plugins: [
resolve({browser: true}),
commonjs()
],
output: {
format: 'iife',
name: '_axios',
sourcemap: 'inline'
}
},


Expand Down
4 changes: 1 addition & 3 deletions lib/adapters/http.js
Expand Up @@ -11,7 +11,7 @@ import httpFollow from 'follow-redirects/http.js';
import httpsFollow from 'follow-redirects/https.js';
import url from 'url';
import zlib from 'zlib';
import envData from './../env/data.js';
import {VERSION} from '../env/data.js';
import transitionalDefaults from '../defaults/transitional.js';
import AxiosError from '../core/AxiosError.js';
import CanceledError from '../cancel/CanceledError.js';
Expand All @@ -22,8 +22,6 @@ import AxiosHeaders from '../core/AxiosHeaders.js';
import AxiosTransformStream from '../helpers/AxiosTransformStream.js';
import EventEmitter from 'events';

const VERSION = envData.version;

const isBrotliSupported = utils.isFunction(zlib.createBrotliDecompress);

const isHttps = /https:?/;
Expand Down
7 changes: 3 additions & 4 deletions lib/axios.js
Expand Up @@ -9,13 +9,12 @@ import formDataToJSON from './helpers/formDataToJSON.js';
import CanceledError from './cancel/CanceledError.js';
import CancelToken from'./cancel/CancelToken.js';
import isCancel from'./cancel/isCancel.js';
import envData from './env/data.js';
import {VERSION} from './env/data.js';
import toFormData from './helpers/toFormData.js';
import AxiosError from '../lib/core/AxiosError.js';
import spread from './helpers/spread.js';
import isAxiosError from './helpers/isAxiosError.js';

const VERSION = envData.version;
/**
* Create an instance of Axios
*
Expand All @@ -28,10 +27,10 @@ function createInstance(defaultConfig) {
const instance = bind(Axios.prototype.request, context);

// Copy axios.prototype to instance
utils.extend(instance, Axios.prototype, context);
utils.extend(instance, Axios.prototype, context, {allOwnKeys: true});

// Copy context to instance
utils.extend(instance, context);
utils.extend(instance, context, {allOwnKeys: true});

// Factory for creating new instances
instance.create = function create(instanceConfig) {
Expand Down
4 changes: 1 addition & 3 deletions lib/env/data.js
@@ -1,3 +1 @@
export default {
"version": "1.0.0-alpha.1"
};
export const VERSION = "1.0.0-alpha.1";
4 changes: 1 addition & 3 deletions lib/helpers/validator.js
@@ -1,10 +1,8 @@
'use strict';

import envData from '../env/data.js';
import {VERSION} from '../env/data.js';
import AxiosError from '../core/AxiosError.js';

const VERSION = envData.version;

const validators = {};

// eslint-disable-next-line func-names
Expand Down

0 comments on commit 462ed4c

Please sign in to comment.