Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions __tests__/devenv-e2e/002-destroy.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it, jest } from '@jest/globals';
import Docker from 'dockerode';
import nock from 'nock';
import { access, mkdtemp, rm, unlink } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
Expand Down Expand Up @@ -28,9 +27,6 @@ describe( 'vip dev-env destroy', () => {
let tmpPath;

beforeAll( async () => {
nock.cleanAll();
nock.enableNetConnect();

cliTest = new CliTest();

tmpPath = await mkdtemp( path.join( os.tmpdir(), 'vip-dev-env-' ) );
Expand All @@ -40,7 +36,6 @@ describe( 'vip dev-env destroy', () => {
} );

afterAll( () => rm( tmpPath, { recursive: true, force: true } ) );
afterAll( () => nock.restore() );

it( 'should fail if environment does not exist', async () => {
const slug = getProjectSlug();
Expand Down
5 changes: 0 additions & 5 deletions __tests__/devenv-e2e/003-start.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it, jest } from '@jest/globals';
import Docker from 'dockerode';
import nock from 'nock';
import { mkdtemp, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
Expand Down Expand Up @@ -31,9 +30,6 @@ describe( 'vip dev-env start', () => {
let slug;

beforeAll( async () => {
nock.cleanAll();
nock.enableNetConnect();

cliTest = new CliTest();

tmpPath = await mkdtemp( path.join( os.tmpdir(), 'vip-dev-env-' ) );
Expand All @@ -45,7 +41,6 @@ describe( 'vip dev-env start', () => {
} );

afterAll( () => rm( tmpPath, { recursive: true, force: true } ) );
afterAll( () => nock.restore() );

afterEach( () => killProjectContainers( docker, slug ) );

Expand Down
5 changes: 0 additions & 5 deletions __tests__/devenv-e2e/004-stop.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { afterAll, describe, expect, it, jest } from '@jest/globals';
import Docker from 'dockerode';
import nock from 'nock';
import { mkdtemp, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
Expand Down Expand Up @@ -31,9 +30,6 @@ describe( 'vip dev-env stop', () => {
let slug;

beforeAll( async () => {
nock.cleanAll();
nock.enableNetConnect();

cliTest = new CliTest();

tmpPath = await mkdtemp( path.join( os.tmpdir(), 'vip-dev-env-' ) );
Expand All @@ -45,7 +41,6 @@ describe( 'vip dev-env stop', () => {
} );

afterAll( () => rm( tmpPath, { recursive: true, force: true } ) );
afterAll( () => nock.restore() );

afterEach( () => killProjectContainers( docker, slug ) );

Expand Down
6 changes: 0 additions & 6 deletions __tests__/devenv-e2e/006-list.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it, jest } from '@jest/globals';
import Docker from 'dockerode';
import nock from 'nock';
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
Expand Down Expand Up @@ -30,14 +29,9 @@ describe( 'vip dev-env list', () => {
let tmpPath;

beforeAll( () => {
nock.cleanAll();
nock.enableNetConnect();

cliTest = new CliTest();
} );

afterAll( () => nock.restore() );

beforeEach( async () => {
tmpPath = await mkdtemp( path.join( os.tmpdir(), 'vip-dev-env-' ) );
process.env.XDG_DATA_HOME = tmpPath;
Expand Down
6 changes: 0 additions & 6 deletions __tests__/devenv-e2e/007-info.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it, jest } from '@jest/globals';
import Docker from 'dockerode';
import nock from 'nock';
import { mkdtemp, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
Expand All @@ -24,14 +23,9 @@ describe( 'vip dev-env info', () => {
let tmpPath;

beforeAll( () => {
nock.cleanAll();
nock.enableNetConnect();

cliTest = new CliTest();
} );

afterAll( () => nock.restore() );

beforeEach( async () => {
tmpPath = await mkdtemp( path.join( os.tmpdir(), 'vip-dev-env-' ) );
process.env.XDG_DATA_HOME = tmpPath;
Expand Down
5 changes: 0 additions & 5 deletions __tests__/devenv-e2e/008-exec.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it, jest } from '@jest/globals';
import Docker from 'dockerode';
import nock from 'nock';
import { mkdtemp, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
Expand All @@ -27,9 +26,6 @@ describe( 'vip dev-env exec', () => {
let tmpPath;

beforeAll( async () => {
nock.cleanAll();
nock.enableNetConnect();

cliTest = new CliTest();

tmpPath = await mkdtemp( path.join( os.tmpdir(), 'vip-dev-env-' ) );
Expand All @@ -39,7 +35,6 @@ describe( 'vip dev-env exec', () => {
} );

afterAll( () => rm( tmpPath, { recursive: true, force: true } ) );
afterAll( () => nock.restore() );

describe( 'if the environment does not exist', () => {
it( 'should fail', async () => {
Expand Down
5 changes: 0 additions & 5 deletions __tests__/devenv-e2e/010-import-sql.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it, jest } from '@jest/globals';
import Docker from 'dockerode';
import nock from 'nock';
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
Expand All @@ -27,9 +26,6 @@ describe( 'vip dev-env import sql', () => {
let tmpPath;

beforeAll( async () => {
nock.cleanAll();
nock.enableNetConnect();

cliTest = new CliTest();

tmpPath = await mkdtemp( path.join( os.tmpdir(), 'vip-dev-env-' ) );
Expand All @@ -39,7 +35,6 @@ describe( 'vip dev-env import sql', () => {
} );

afterAll( () => rm( tmpPath, { recursive: true, force: true } ) );
afterAll( () => nock.restore() );

describe( 'if the environment does not exist', () => {
it( 'should fail', async () => {
Expand Down
5 changes: 0 additions & 5 deletions __tests__/devenv-e2e/011-logs.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it } from '@jest/globals';
import Docker from 'dockerode';
import nock from 'nock';
import { mkdtemp, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
Expand All @@ -25,9 +24,6 @@ describe( 'vip dev-env logs', () => {
let tmpPath;

beforeAll( async () => {
nock.cleanAll();
nock.enableNetConnect();

cliTest = new CliTest();

tmpPath = await mkdtemp( path.join( os.tmpdir(), 'vip-dev-env-' ) );
Expand All @@ -37,7 +33,6 @@ describe( 'vip dev-env logs', () => {
} );

afterAll( () => rm( tmpPath, { recursive: true, force: true } ) );
afterAll( () => nock.restore() );

describe( 'if the environment does not exist', () => {
it( 'should fail', async () => {
Expand Down
5 changes: 0 additions & 5 deletions __tests__/devenv-e2e/012-shell.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it, jest } from '@jest/globals';
import Docker from 'dockerode';
import nock from 'nock';
import { mkdtemp, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
Expand All @@ -27,9 +26,6 @@ describe( 'vip dev-env shell', () => {
let tmpPath;

beforeAll( async () => {
nock.cleanAll();
nock.enableNetConnect();

cliTest = new CliTest();

tmpPath = await mkdtemp( path.join( os.tmpdir(), 'vip-dev-env-' ) );
Expand All @@ -39,7 +35,6 @@ describe( 'vip dev-env shell', () => {
} );

afterAll( () => rm( tmpPath, { recursive: true, force: true } ) );
afterAll( () => nock.restore() );

describe( 'if the environment does not exist', () => {
it( 'should fail', async () => {
Expand Down
51 changes: 35 additions & 16 deletions __tests__/lib/analytics/clients/tracks.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import nock from 'nock';

import Tracks from '../../../../src/lib/analytics/clients/tracks';
import * as apiConfig from '../../../../src/lib/cli/apiConfig';
import { getUndiciMockPool, resetUndiciMockAgent } from '../../../../test-utils/undici-mock';

describe( 'lib/analytics/tracks', () => {
const url = new URL( Tracks.ENDPOINT );
const pool = getUndiciMockPool( url.origin );

const buildNock = () => nock( url.origin ).post( url.pathname );
const buildMockRequest = () => pool.intercept( { method: 'POST', path: url.pathname } );

afterEach( nock.cleanAll );
afterEach( resetUndiciMockAgent );

describe( '.send()', () => {
it( 'should correctly construct remote request', () => {
Expand All @@ -24,13 +24,22 @@ describe( 'lib/analytics/tracks', () => {
'&commonProps%5B_via_ua%5D=vip-cli' +
'&extra=param';

buildNock()
// No arrow function because we need `this`
.reply( 200, function ( uri, requestBody ) {
expect( this.req.headers[ 'user-agent' ] ).toEqual( 'vip-cli' ); // The header value is returned as a string

expect( requestBody ).toEqual( expectedBody );
} );
buildMockRequest().reply( options => {
const headers = Object.fromEntries(
Object.entries( options.headers ?? {} ).map( ( [ key, value ] ) => [
key.toLowerCase(),
String( value ),
] )
);

expect( headers[ 'user-agent' ] ).toEqual( 'vip-cli' );
expect( String( options.body ) ).toEqual( expectedBody );

return {
statusCode: 200,
data: 'ok',
};
} );

return tracksClient.send( params );
} );
Expand All @@ -53,8 +62,13 @@ describe( 'lib/analytics/tracks', () => {
'&events%5B0%5D%5BbuttonName%5D=deploy' +
'&events%5B0%5D%5Bis_vip%5D=true';

buildNock().reply( 200, ( uri, requestBody ) => {
expect( requestBody ).toContain( expectedBodyMatch );
buildMockRequest().reply( options => {
expect( String( options.body ) ).toContain( expectedBodyMatch );

return {
statusCode: 200,
data: 'ok',
};
} );

return tracksClient.trackEvent( eventName, eventDetails );
Expand All @@ -67,8 +81,13 @@ describe( 'lib/analytics/tracks', () => {

const expectedBodyMatch = 'events%5B0%5D%5B_en%5D=existingprefix_clickButton';

buildNock().reply( 200, ( uri, requestBody ) => {
expect( requestBody ).toContain( expectedBodyMatch );
buildMockRequest().reply( options => {
expect( String( options.body ) ).toContain( expectedBodyMatch );

return {
statusCode: 200,
data: 'ok',
};
} );

return tracksClient.trackEvent( eventName, {} );
Expand All @@ -79,7 +98,7 @@ describe( 'lib/analytics/tracks', () => {

const eventName = 'existingprefix_clickButton';

buildNock().replyWithError( 'Connection reset' );
buildMockRequest().replyWithError( 'Connection reset' );

// We expect that the promise resolves to false instead of rejecting and throwing errors with async/await
await expect( tracksClient.trackEvent( eventName, {} ) ).resolves.toBe( false );
Expand Down
5 changes: 2 additions & 3 deletions __tests__/lib/api-retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ApolloClient, ApolloLink, ServerError } from '@apollo/client/core';
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import { OperationTypeNode } from 'graphql';
import gql from 'graphql-tag';
import { FetchError } from 'node-fetch';

import { shouldRetryRequest } from '../../src/lib/api';

Expand Down Expand Up @@ -88,7 +87,7 @@ describe( 'API Retry Logic', () => {
it( 'should retry on ECONNREFUSED errors', () => {
const error = Object.assign( new Error( 'Connection refused' ), {
code: 'ECONNREFUSED',
} ) as FetchError;
} );

const result = shouldRetryRequest( 1, mockOperation, error );

Expand Down Expand Up @@ -153,7 +152,7 @@ describe( 'API Retry Logic', () => {
it( 'should handle FetchError without ECONNREFUSED code', () => {
const error = Object.assign( new Error( 'Network error' ), {
code: 'ENOTFOUND',
} ) as FetchError;
} );

const result = shouldRetryRequest( 1, mockOperation, error );

Expand Down
21 changes: 20 additions & 1 deletion __tests__/lib/client-file-uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
* @format
*/

import { getFileHash, getFileMeta, getPartBoundaries } from '../../src/lib/client-file-uploader';
import {
getFileHash,
getFileMeta,
getPartBoundaries,
parseEtagHeader,
} from '../../src/lib/client-file-uploader';

describe( 'client-file-uploader', () => {
describe( 'getFileMeta()', () => {
Expand Down Expand Up @@ -90,4 +95,18 @@ describe( 'client-file-uploader', () => {
] );
} );
} );

describe( 'parseEtagHeader()', () => {
it( 'should parse a quoted ETag header', () => {
expect( parseEtagHeader( '"abc123"' ) ).toBe( 'abc123' );
} );

it( 'should strip a weak ETag prefix', () => {
expect( parseEtagHeader( 'W/"abc123"' ) ).toBe( 'abc123' );
} );

it( 'should return an unquoted ETag value as-is', () => {
expect( parseEtagHeader( 'abc123' ) ).toBe( 'abc123' );
} );
} );
} );
Loading
Loading