dotenv#parse TypeScript Examples
The following examples show how to use
dotenv#parse.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: config.service.ts From nest-react with GNU Lesser General Public License v3.0 | 6 votes |
/**
* Extract the configuration from a `dotenv` file
* @param env The environment name. Corresponding `name.env` file will be used. Default to `local`
*/
private getConfigFromEnvFile(env = 'local'): DotenvParseOutput {
const envFilePath = join('env', `.env.${env}`);
try {
const config = parse(readFileSync(envFilePath));
return config;
} catch (err) {
const msg = `Configuration error, see below:
/!\\ No environment definition found at ${envFilePath}. Please choose one of the following options (in preference order):
1. Set both the CONFIG_PATH and the SECRETS_PATH environment variables and fill their respective folders with corresponding environment values.
2. Set the NODE_ENV environment variable and attach the corresponding "dotenv" file to the server.
`;
this.logger.error(msg);
throw new InternalServerErrorException();
}
}
Example #2
Source File: evm.ts From airnode with MIT License | 6 votes |
deployContract = async (artifactsFolderPath: string, args: any[] = []) => {
const artifact = getArtifact(artifactsFolderPath);
// Deploy the contract
const contractFactory = new ethers.ContractFactory(artifact.abi, artifact.bytecode, await getUserWallet());
const contract = await contractFactory.deploy(...args);
await contract.deployed();
// Make sure the deployments folder exist
const deploymentsPath = join(__dirname, '../deployments');
if (!existsSync(deploymentsPath)) mkdirSync(deploymentsPath);
// Try to load the existing deployments file for this network - we want to preserve deployments of other contracts
const network = readIntegrationInfo().network;
const deploymentPath = join(deploymentsPath, network + '.json');
let deployment: any = {};
if (existsSync(deploymentPath)) deployment = JSON.parse(readFileSync(deploymentPath).toString());
// The key name for this contract is the path of the artifact without the '.sol' extension
const deploymentName = removeExtension(artifactsFolderPath);
// Write down the address of deployed contract
writeFileSync(deploymentPath, JSON.stringify({ ...deployment, [deploymentName]: contract.address }, null, 2));
return contract;
}
Example #3
Source File: evm.ts From airnode with MIT License | 6 votes |
getDeployedContract = async (artifactsFolderPath: string) => {
const artifact = getArtifact(artifactsFolderPath);
const network = readIntegrationInfo().network;
const deploymentPath = join(__dirname, '../deployments', network + '.json');
const deployment = JSON.parse(readFileSync(deploymentPath).toString());
const deploymentName = removeExtension(artifactsFolderPath);
return new ethers.Contract(deployment[deploymentName], artifact.abi, await getUserWallet());
}
Example #4
Source File: file.ts From cli with Apache License 2.0 | 6 votes |
export function saveToEnvFile(
pathToEnv: string,
additionalEnvironment: Record<string, unknown>
) {
ensureFileSync(pathToEnv);
const environment = parse(readFileSync(pathToEnv, {encoding: 'utf-8'}));
const updatedEnvironment = {
...environment,
...additionalEnvironment,
};
truncateSync(pathToEnv);
for (const [key, value] of Object.entries(updatedEnvironment)) {
appendFileSync(pathToEnv, `${key}=${value}${EOL}`);
}
}
Example #5
Source File: test.ts From dropify with MIT License | 5 votes |
parsed = parse("NODE_ENV=production\nDB_HOST=a.b.c")
Example #6
Source File: test.ts From dropify with MIT License | 5 votes |
parsedFromBuffer = parse(new Buffer("JUSTICE=league\n"), {
debug: true
})
Example #7
Source File: config.ts From nest_transact with MIT License | 5 votes |
envConfig = parse(readFileSync(envPath))
Example #8
Source File: evm.ts From airnode with MIT License | 5 votes |
getAirnodeWallet = () => {
const integrationInfo = readIntegrationInfo();
const integrationSecrets = parse(
readFileSync(join(__dirname, `../integrations/${integrationInfo.integration}/secrets.env`))
);
return ethers.Wallet.fromMnemonic(integrationSecrets['AIRNODE_WALLET_MNEMONIC']);
}
Example #9
Source File: angular.specs.ts From cli with Apache License 2.0 | 4 votes |
// TODO CDX-804: Enable the tests back
describe('ui:create:angular', () => {
let browser: Browser;
const processManagers: ProcessManager[] = [];
let page: Page;
const oldEnv = process.env;
const parentDir = 'angular';
const projectName = `${process.env.TEST_RUN_ID}-${parentDir}-project`;
const projectPath = join(getUIProjectPath(), parentDir, projectName);
let clientPort: number;
let serverPort: number;
const angularJsonPath = join(projectPath, 'angular.json');
const searchPageEndpoint = () => `http://localhost:${clientPort}`;
const tokenServerEndpoint = () => `http://localhost:${serverPort}/token`;
const setCustomTokenEndpoint = (endpoint: string) => {
const webAppEnvironment = resolve(
projectPath,
'src',
'environments',
'environment.ts'
);
const portNumberMatcher = /customTokenEndpoint:\s?.*$/m;
const appEnvironmentFile = readFileSync(webAppEnvironment, 'utf-8');
const subst = `customTokenEndpoint: '${endpoint}',`;
const updatedEnvironment = appEnvironmentFile.replace(
portNumberMatcher,
subst
);
writeFileSync(webAppEnvironment, updatedEnvironment);
};
const resetCustomTokenEndpoint = () => setCustomTokenEndpoint('');
const forceTokenServerPort = (port: number) => {
const pathToEnv = resolve(projectPath, 'server', '.env');
const environment = parse(readFileSync(pathToEnv, {encoding: 'utf-8'}));
const updatedEnvironment = {
...environment,
SERVER_PORT: port,
};
truncateSync(pathToEnv);
for (const [key, value] of Object.entries(updatedEnvironment)) {
appendFileSync(pathToEnv, `${key}=${value}${EOL}`);
}
};
const forceAppPort = (port: number) => {
const angularJSON = JSON.parse(readFileSync(angularJsonPath, 'utf-8'));
const serve = angularJSON.projects[projectName].architect.serve;
if (!serve.options) {
serve.options = {};
}
serve.options.port = port;
writeFileSync(angularJsonPath, JSON.stringify(angularJSON, undefined, 2));
};
const getAllocatedPorts = () => {
const envVariables = parse(
readFileSync(getPathToEnvFile(join(projectPath, 'server')))
);
if (!envVariables) {
throw new Error('Unable to load project environment variables');
}
serverPort = parseInt(envVariables.SERVER_PORT);
const angularJSON = JSON.parse(readFileSync(angularJsonPath, 'utf-8'));
const servePort =
angularJSON.projects[projectName].architect.serve.options.port;
clientPort = parseInt(servePort);
return [clientPort, serverPort];
};
const buildApplication = async (processManager: ProcessManager) => {
const buildTerminal = await setupUIProject(
processManager,
'ui:create:angular',
projectName,
{
flags: ['--defaults'],
projectDir: projectPath,
}
);
const buildTerminalExitPromise = Promise.race([
buildTerminal.when('exit').on('process').do().once(),
buildTerminal
.when(/Happy hacking!/)
.on('stdout')
.do()
.once(),
]);
await buildTerminal
.when(isGenericYesNoPrompt)
.on('stderr')
.do(answerPrompt(`y${EOL}`))
.until(buildTerminalExitPromise);
};
const startApplication = async (
processManager: ProcessManager,
debugName = 'angular-server'
) => {
const args = [...npm(), 'run', 'start'];
const serverTerminal = new Terminal(
args.shift()!,
args,
{
cwd: projectPath,
},
processManager,
debugName
);
return serverTerminal;
};
const waitForAppRunning = (appTerminal: Terminal) =>
appTerminal
.when(/Compiled successfully/)
.on('stdout')
.do()
.once();
beforeAll(async () => {
await loginWithApiKey(
process.env.PLATFORM_API_KEY!,
process.env.ORG_ID!,
process.env.PLATFORM_ENV!
);
const buildProcessManager = new ProcessManager();
processManagers.push(buildProcessManager);
browser = await getNewBrowser();
await buildApplication(buildProcessManager);
await buildProcessManager.killAllProcesses();
}, 20 * 60e3);
beforeEach(async () => {
jest.resetModules();
process.env = {...oldEnv};
page = await openNewPage(browser, page);
});
afterEach(async () => {
await captureScreenshots(browser);
});
afterAll(async () => {
process.env = oldEnv;
await browser.close();
await Promise.all(
processManagers.map((manager) => manager.killAllProcesses())
);
});
describe('when the project is configured correctly', () => {
let serverProcessManager: ProcessManager;
let interceptedRequests: HTTPRequest[] = [];
let consoleInterceptor: BrowserConsoleInterceptor;
const searchboxSelector = 'app-search-page app-search-box input';
beforeAll(async () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
const appTerminal = await startApplication(
serverProcessManager,
'angular-server-valid'
);
await waitForAppRunning(appTerminal);
[clientPort, serverPort] = getAllocatedPorts();
}, 15 * 60e3);
beforeEach(async () => {
consoleInterceptor = new BrowserConsoleInterceptor(page, projectName);
await consoleInterceptor.startSession();
page.on('request', (request: HTTPRequest) => {
interceptedRequests.push(request);
});
});
afterEach(async () => {
page.removeAllListeners('request');
interceptedRequests = [];
await consoleInterceptor.endSession();
});
afterAll(async () => {
await undoCommit(serverProcessManager, projectPath, projectName);
await serverProcessManager.killAllProcesses();
}, 5 * 60e3);
// TODO CDX-1017: Remove skip
it.skip(
'should not contain console errors nor warnings',
async () => {
await page.goto(searchPageEndpoint(), {
waitUntil: 'networkidle2',
});
expect(consoleInterceptor.interceptedMessages).toEqual([]);
},
5 * 60e3
);
it('should contain a search page section', async () => {
await page.goto(searchPageEndpoint(), {
waitUntil: 'networkidle2',
});
await page.waitForSelector(searchboxSelector);
expect(await page.$('app-search-page')).not.toBeNull();
}, 60e3);
it('should retrieve the search token on the page load', async () => {
const tokenResponseListener = page.waitForResponse(tokenServerEndpoint());
page.goto(searchPageEndpoint());
await page.waitForSelector(searchboxSelector);
expect(
JSON.parse(await (await tokenResponseListener).text())
).toMatchObject({
token: expect.stringMatching(jwtTokenPattern),
});
}, 60e3);
it('should send a search query when the page is loaded', async () => {
await page.goto(searchPageEndpoint(), {waitUntil: 'networkidle2'});
await page.waitForSelector(searchboxSelector);
expect(interceptedRequests.some(isSearchRequestOrResponse)).toBeTruthy();
});
it('should send a search query on searchbox submit', async () => {
await page.goto(searchPageEndpoint(), {waitUntil: 'networkidle2'});
await page.waitForSelector(searchboxSelector);
interceptedRequests = [];
await page.focus(searchboxSelector);
await page.keyboard.type('my query');
await page.keyboard.press('Enter');
await retry(async () => {
expect(
interceptedRequests.some(isSearchRequestOrResponse)
).toBeTruthy();
});
}, 60e3);
it('should be commited without lint-stage errors', async () => {
const eslintErrorSpy = jest.fn();
await commitProject(
serverProcessManager,
projectPath,
projectName,
eslintErrorSpy
);
expect(eslintErrorSpy).not.toBeCalled();
}, 60e3);
});
describe('when the .env file is missing', () => {
let serverProcessManager: ProcessManager;
beforeAll(async () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
deactivateEnvironmentFile(join(projectPath, 'server'));
});
afterAll(async () => {
restoreEnvironmentFile(join(projectPath, 'server'));
await serverProcessManager.killAllProcesses();
}, 30e3);
it(
'should not start the application',
async () => {
const missingEnvErrorSpy = jest.fn();
const appTerminal = await startApplication(
serverProcessManager,
'angular-server-missing-env'
);
await appTerminal
.when(/\.env file not found in the project root/)
.on('stderr')
.do(missingEnvErrorSpy)
.once();
expect(missingEnvErrorSpy).toHaveBeenCalled();
},
2 * 60e3
);
});
describe('when the required environment variables are missing', () => {
let serverProcessManager: ProcessManager;
let envFileContent = '';
beforeAll(async () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
envFileContent = flushEnvFile(join(projectPath, 'server'));
const appTerminal = await startApplication(
serverProcessManager,
'angular-server-invalid'
);
await waitForAppRunning(appTerminal);
[clientPort, serverPort] = getAllocatedPorts();
}, 2 * 60e3);
afterAll(async () => {
overwriteEnvFile(join(projectPath, 'server'), envFileContent);
await serverProcessManager.killAllProcesses();
}, 30e3);
it('should redirect the user to an error page', async () => {
await page.goto(searchPageEndpoint(), {waitUntil: 'networkidle2'});
expect(page.url()).toEqual(`${searchPageEndpoint()}/error`);
});
});
describe('when the a custom token Endpoint is specified', () => {
const customTokenEndpoint = 'http://dummyendpoint.com/some-kind-of-path';
let serverProcessManager: ProcessManager;
let interceptedRequests: HTTPRequest[] = [];
beforeAll(async () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
setCustomTokenEndpoint(customTokenEndpoint);
const appTerminal = await startApplication(
serverProcessManager,
'angular-server-port-test'
);
await waitForAppRunning(appTerminal);
[clientPort, serverPort] = getAllocatedPorts();
}, 2 * 60e3);
afterAll(async () => {
await serverProcessManager.killAllProcesses();
resetCustomTokenEndpoint();
}, 30e3);
beforeEach(async () => {
page.on('request', (request: HTTPRequest) => {
interceptedRequests.push(request);
});
});
afterEach(async () => {
page.removeAllListeners('request');
interceptedRequests = [];
});
it('should use the custom token endpoint', async () => {
page.goto(searchPageEndpoint());
await retry(async () => {
expect(
interceptedRequests.some(
(request: HTTPRequest) => request.url() === customTokenEndpoint
)
).toBeTruthy();
});
}, 10e3);
});
describe('when the ports are busy', () => {
const dummyServers: DummyServer[] = [];
let serverProcessManager: ProcessManager;
let usedClientPort: number;
let usedServerPort: number;
beforeAll(async () => {
usedClientPort = await getPort();
usedServerPort = await getPort();
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
forceAppPort(usedClientPort);
forceTokenServerPort(usedServerPort);
dummyServers.push(
new DummyServer(usedClientPort),
new DummyServer(usedServerPort)
);
const appTerminal = await startApplication(
serverProcessManager,
'angular-server-port-test'
);
await waitForAppRunning(appTerminal);
[clientPort, serverPort] = getAllocatedPorts();
}, 2 * 60e3);
afterAll(async () => {
await Promise.all(dummyServers.map((server) => server.close()));
await serverProcessManager.killAllProcesses();
}, 30e3);
it('should allocate a new port for the application', async () => {
expect(clientPort).not.toEqual(usedClientPort);
});
it('should not use an undefined port for application', async () => {
expect(clientPort).not.toBeUndefined();
});
it('should allocate a new port for the token server', async () => {
expect(serverPort).not.toEqual(usedServerPort);
});
it('should not use an undefined port for token server', async () => {
expect(serverPort).not.toBeUndefined();
});
it('should run the application on a new port', async () => {
await expect(
page.goto(searchPageEndpoint(), {waitUntil: 'load'})
).resolves.not.toThrow();
});
it('should run the server on a new port', async () => {
const tokenRequest = await axios.get(tokenServerEndpoint());
expect(tokenRequest.data.token).toMatch(jwtTokenPattern);
});
});
});
Example #10
Source File: react.specs.ts From cli with Apache License 2.0 | 4 votes |
describe('ui:create:react', () => {
let browser: Browser;
const processManagers: ProcessManager[] = [];
let page: Page;
const oldEnv = process.env;
const parentDir = 'react';
const projectName = `${process.env.TEST_RUN_ID}-react-project`;
const projectPath = join(getUIProjectPath(), parentDir, projectName);
let clientPort: number;
let serverPort: number;
const searchPageEndpoint = () => `http://localhost:${clientPort}`;
const tokenServerEndpoint = () => `http://localhost:${serverPort}/token`;
const waitForAppRunning = (appTerminal: Terminal) =>
appTerminal
.when(/You can now view .*-react-project in the browser/)
.on('stdout')
.do()
.once();
const forceApplicationPorts = (clientPort: number, serverPort: number) => {
const envPath = getPathToEnvFile(projectPath);
const environment = parse(readFileSync(envPath, {encoding: 'utf-8'}));
const updatedEnvironment = {
...environment,
PORT: clientPort,
REACT_APP_SERVER_PORT: serverPort,
};
truncateSync(envPath);
for (const [key, value] of Object.entries(updatedEnvironment)) {
appendFileSync(envPath, `${key}=${value}${EOL}`);
}
};
const getAllocatedPorts = () => {
const envVariables = parse(
readFileSync(getPathToEnvFile(projectPath), {encoding: 'utf-8'})
);
if (!envVariables) {
throw new Error('Unable to load project environment variables');
}
clientPort = parseInt(envVariables.PORT);
serverPort = parseInt(envVariables.REACT_APP_SERVER_PORT);
return [clientPort, serverPort];
};
const buildApplication = async (processManager: ProcessManager) => {
const buildTerminal = await setupUIProject(
processManager,
'ui:create:react',
projectName,
{projectDir: projectPath}
);
await buildTerminal.when('exit').on('process').do().once();
};
const startApplication = async (
processManager: ProcessManager,
debugName = 'react-server'
) => {
const args = [...npm(), 'run', 'start'];
const serverTerminal = new Terminal(
args.shift()!,
args,
{
cwd: projectPath,
},
processManager,
debugName
);
return serverTerminal;
};
beforeAll(async () => {
await loginWithApiKey(
process.env.PLATFORM_API_KEY!,
process.env.ORG_ID!,
process.env.PLATFORM_ENV!
);
const buildProcessManager = new ProcessManager();
processManagers.push(buildProcessManager);
browser = await getNewBrowser();
await buildApplication(buildProcessManager);
await buildProcessManager.killAllProcesses();
}, 15 * 60e3);
beforeEach(async () => {
jest.resetModules();
process.env = {...oldEnv};
page = await openNewPage(browser, page);
});
afterEach(async () => {
await captureScreenshots(browser);
});
afterAll(async () => {
process.env = oldEnv;
await browser.close();
await Promise.all(
processManagers.map((manager) => manager.killAllProcesses())
);
});
describe('when the project is configured correctly', () => {
let serverProcessManager: ProcessManager;
let interceptedRequests: HTTPRequest[] = [];
let consoleInterceptor: BrowserConsoleInterceptor;
const searchboxSelector = 'div.App .MuiAutocomplete-root input';
beforeAll(async () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
const appTerminal = await startApplication(
serverProcessManager,
'react-server-valid'
);
await waitForAppRunning(appTerminal);
[clientPort, serverPort] = getAllocatedPorts();
}, 10 * 60e3);
beforeEach(async () => {
consoleInterceptor = new BrowserConsoleInterceptor(page, projectPath);
await consoleInterceptor.startSession();
page.on('request', (request: HTTPRequest) => {
interceptedRequests.push(request);
});
});
afterEach(async () => {
page.removeAllListeners('request');
interceptedRequests = [];
await consoleInterceptor.endSession();
});
afterAll(async () => {
await serverProcessManager.killAllProcesses();
}, 30e3);
// TODO CDX-1017: Remove skip
it.skip(
'should not contain console errors nor warnings',
async () => {
await page.goto(searchPageEndpoint(), {
waitUntil: 'networkidle2',
});
expect(consoleInterceptor.interceptedMessages).toEqual([]);
},
5 * 60e3
);
it('should contain a search page section', async () => {
await page.goto(searchPageEndpoint(), {
waitUntil: 'networkidle2',
});
await page.waitForSelector(searchboxSelector);
expect(await page.$('div.App')).not.toBeNull();
});
it('should retrieve the search token on the page load', async () => {
const tokenResponseListener = page.waitForResponse(tokenServerEndpoint());
page.goto(searchPageEndpoint());
await page.waitForSelector(searchboxSelector);
expect(
JSON.parse(await (await tokenResponseListener).text())
).toMatchObject({
token: expect.stringMatching(jwtTokenPattern),
});
});
it('should send a search query when the page is loaded', async () => {
await page.goto(searchPageEndpoint(), {waitUntil: 'networkidle2'});
await page.waitForSelector(searchboxSelector);
expect(interceptedRequests.some(isSearchRequestOrResponse)).toBeTruthy();
});
it('should send a search query on searchbox submit', async () => {
await page.goto(searchPageEndpoint(), {waitUntil: 'networkidle2'});
await page.waitForSelector(searchboxSelector);
interceptedRequests = [];
await page.focus(searchboxSelector);
await page.keyboard.type('my query');
await page.keyboard.press('Enter');
await retry(async () => {
expect(
interceptedRequests.some(isSearchRequestOrResponse)
).toBeTruthy();
});
});
it('should have a clean working directory', async () => {
const gitDirtyWorkingTreeSpy = jest.fn();
await isDirectoryClean(
serverProcessManager,
projectPath,
projectName,
gitDirtyWorkingTreeSpy
);
expect(gitDirtyWorkingTreeSpy).not.toBeCalled();
}, 10e3);
});
describe('when the .env file is missing', () => {
let serverProcessManager: ProcessManager;
beforeAll(async () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
deactivateEnvironmentFile(projectPath);
});
afterAll(async () => {
restoreEnvironmentFile(projectPath);
await serverProcessManager.killAllProcesses();
}, 30e3);
it(
'should not start the application',
async () => {
const missingEnvErrorSpy = jest.fn();
const appTerminal = await startApplication(
serverProcessManager,
'react-server-missing-env'
);
await appTerminal
.when(/\.env file not found in the project root/)
.on('stderr')
.do(missingEnvErrorSpy)
.once();
expect(missingEnvErrorSpy).toHaveBeenCalled();
},
10 * 60e3
);
});
describe('when required environment variables are not defined', () => {
let serverProcessManager: ProcessManager;
let envFileContent = '';
const errorMessageSelector = 'div.container';
beforeAll(async () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
envFileContent = flushEnvFile(projectPath);
overwriteEnvFile(projectPath, 'GENERATE_SOURCEMAP=false'); // TODO: CDX-737: fix exponential-backoff compilation warnings
const appTerminal = await startApplication(
serverProcessManager,
'react-server-invalid'
);
await waitForAppRunning(appTerminal);
[clientPort, serverPort] = getAllocatedPorts();
}, 2 * 60e3);
afterAll(async () => {
overwriteEnvFile(projectPath, envFileContent);
await serverProcessManager.killAllProcesses();
}, 30e3);
it('should redirect the user to an error page', async () => {
await page.goto(searchPageEndpoint(), {waitUntil: 'networkidle2'});
const pageErrorMessage = await page.$eval(
errorMessageSelector,
(el) => el.textContent
);
expect(pageErrorMessage).toContain(
'You should have a valid .env file at the root of this project'
);
});
});
describe('when the ports are manually specified', () => {
let serverProcessManager: ProcessManager;
let hardCodedClientPort: number;
let hardCodedServerPort: number;
beforeAll(async () => {
hardCodedClientPort = await getPort();
hardCodedServerPort = await getPort();
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
forceApplicationPorts(hardCodedClientPort, hardCodedServerPort);
const appTerminal = await startApplication(
serverProcessManager,
'react-server-port-test'
);
await waitForAppRunning(appTerminal);
[clientPort, serverPort] = getAllocatedPorts();
}, 2 * 60e3);
afterAll(async () => {
await serverProcessManager.killAllProcesses();
}, 30e3);
it('should run the application on the specified port', async () => {
expect(clientPort).toEqual(hardCodedClientPort);
});
it('should run the token server on the specified port', async () => {
expect(serverPort).toEqual(hardCodedServerPort);
});
});
describe('when the ports are busy', () => {
const dummyServers: DummyServer[] = [];
let serverProcessManager: ProcessManager;
let usedClientPort: number;
let usedServerPort: number;
beforeAll(async () => {
usedClientPort = await getPort();
usedServerPort = await getPort();
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
forceApplicationPorts(usedClientPort, usedServerPort);
dummyServers.push(
new DummyServer(usedClientPort),
new DummyServer(usedServerPort)
);
const appTerminal = await startApplication(
serverProcessManager,
'react-server-port-test'
);
await waitForAppRunning(appTerminal);
[clientPort, serverPort] = getAllocatedPorts();
}, 2 * 60e3);
afterAll(async () => {
await Promise.all(dummyServers.map((server) => server.close()));
await serverProcessManager.killAllProcesses();
}, 30e3);
it('should allocate a new port for the application', async () => {
expect(clientPort).not.toEqual(usedClientPort);
});
it('should not use an undefined port for application', async () => {
expect(clientPort).not.toBeUndefined();
});
it('should allocate a new port for the token server', async () => {
expect(serverPort).not.toEqual(usedServerPort);
});
it('should not use an undefined port for token server', async () => {
expect(serverPort).not.toBeUndefined();
});
it('should run the application on a new port', async () => {
await expect(
page.goto(searchPageEndpoint(), {waitUntil: 'load'})
).resolves.not.toThrow();
});
it('should run the server on a new port', async () => {
const tokenRequest = await axios.get(tokenServerEndpoint());
expect(tokenRequest.data.token).toMatch(jwtTokenPattern);
});
});
});
Example #11
Source File: vue.specs.ts From cli with Apache License 2.0 | 4 votes |
describe('ui:create:vue', () => {
let browser: Browser;
const processManagers: ProcessManager[] = [];
let page: Page;
const oldEnv = process.env;
const parentDir = 'vue';
const projectName = `${process.env.TEST_RUN_ID}-${parentDir}-project`;
const projectPath = join(getUIProjectPath(), parentDir, projectName);
let clientPort: number;
let serverPort: number;
const searchPageEndpoint = () => `http://localhost:${clientPort}`;
const tokenServerEndpoint = () => `http://localhost:${serverPort}/token`;
const forceApplicationPorts = (clientPort: number, serverPort: number) => {
const envPath = getPathToEnvFile(projectPath);
const environment = parse(readFileSync(envPath, {encoding: 'utf-8'}));
const updatedEnvironment = {
...environment,
PORT: clientPort,
VUE_APP_SERVER_PORT: serverPort,
};
truncateSync(envPath);
for (const [key, value] of Object.entries(updatedEnvironment)) {
appendFileSync(envPath, `${key}=${value}${EOL}`);
}
};
const waitForAppRunning = (appTerminal: Terminal) =>
appTerminal
.when(/App running at:/)
.on('stdout')
.do()
.once();
const getAllocatedPorts = () => {
const envVariables = parse(
readFileSync(getPathToEnvFile(projectPath), {encoding: 'utf-8'})
);
if (!envVariables) {
throw new Error('Unable to load project environment variables');
}
clientPort = parseInt(envVariables.PORT);
serverPort = parseInt(envVariables.VUE_APP_SERVER_PORT);
return [clientPort, serverPort];
};
const buildApplication = async (processManager: ProcessManager) => {
const buildTerminal = await setupUIProject(
processManager,
'ui:create:vue',
projectName,
{projectDir: projectPath}
);
const buildTerminalExitPromise = Promise.race([
buildTerminal.when('exit').on('process').do().once(),
buildTerminal
.when(/Happy hacking!/)
.on('stdout')
.do()
.once(),
]);
await buildTerminal
.when(isGenericYesNoPrompt)
.on('stdout')
.do(answerPrompt(`y${EOL}`))
.until(buildTerminalExitPromise);
await buildTerminalExitPromise;
};
const startApplication = async (
processManager: ProcessManager,
debugName = 'vue-server'
) => {
const args = [...npm(), 'run', 'start'];
const serverTerminal = new Terminal(
args.shift()!,
args,
{
cwd: projectPath,
},
processManager,
debugName
);
return serverTerminal;
};
beforeAll(async () => {
await loginWithApiKey(
process.env.PLATFORM_API_KEY!,
process.env.ORG_ID!,
process.env.PLATFORM_ENV!
);
const buildProcessManager = new ProcessManager();
processManagers.push(buildProcessManager);
browser = await getNewBrowser();
await buildApplication(buildProcessManager);
await buildProcessManager.killAllProcesses();
}, 15 * 60e3);
beforeEach(async () => {
jest.resetModules();
process.env = {...oldEnv};
page = await openNewPage(browser, page);
});
afterEach(async () => {
await captureScreenshots(browser);
});
afterAll(async () => {
process.env = oldEnv;
await browser.close();
await Promise.all(
processManagers.map((manager) => manager.killAllProcesses())
);
});
describe('when the project is configured correctly', () => {
let serverProcessManager: ProcessManager;
let interceptedRequests: HTTPRequest[] = [];
let consoleInterceptor: BrowserConsoleInterceptor;
const searchboxSelector = '#search-page .autocomplete input';
beforeAll(async () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
const appTerminal = await startApplication(
serverProcessManager,
'vue-server-valid'
);
await waitForAppRunning(appTerminal);
[clientPort, serverPort] = getAllocatedPorts();
}, 2 * 60e3);
beforeEach(async () => {
consoleInterceptor = new BrowserConsoleInterceptor(page, projectName);
await consoleInterceptor.startSession();
page.on('request', (request: HTTPRequest) => {
interceptedRequests.push(request);
});
});
afterEach(async () => {
page.removeAllListeners('request');
interceptedRequests = [];
await consoleInterceptor.endSession();
});
afterAll(async () => {
await undoCommit(serverProcessManager, projectPath, projectName);
await serverProcessManager.killAllProcesses();
}, 5 * 60e3);
// TODO CDX-1017: Remove skip
it.skip('should not contain console errors nor warnings', async () => {
await page.goto(searchPageEndpoint(), {
waitUntil: 'networkidle2',
});
expect(consoleInterceptor.interceptedMessages).toEqual([]);
}, 60e3);
it('should contain a search page section', async () => {
await page.goto(searchPageEndpoint(), {
waitUntil: 'networkidle2',
});
await page.waitForSelector(searchboxSelector);
expect(await page.$('#search-page')).not.toBeNull();
}, 60e3);
it('should retrieve the search token on the page load', async () => {
const tokenResponseListener = page.waitForResponse(tokenServerEndpoint());
page.goto(searchPageEndpoint());
await page.waitForSelector(searchboxSelector);
expect(
JSON.parse(await (await tokenResponseListener).text())
).toMatchObject({
token: expect.stringMatching(jwtTokenPattern),
});
}, 60e3);
it('should send a search query when the page is loaded', async () => {
await page.goto(searchPageEndpoint(), {waitUntil: 'networkidle2'});
await page.waitForSelector(searchboxSelector);
expect(interceptedRequests.some(isSearchRequestOrResponse)).toBeTruthy();
}, 60e3);
it('should send a search query on searchbox submit', async () => {
await page.goto(searchPageEndpoint(), {waitUntil: 'networkidle2'});
await page.waitForSelector(searchboxSelector);
interceptedRequests = [];
await page.focus(searchboxSelector);
await page.keyboard.type('my query');
await page.keyboard.press('Enter');
await retry(async () => {
expect(
interceptedRequests.some(isSearchRequestOrResponse)
).toBeTruthy();
});
}, 60e3);
it('should be commited without lint-stage errors', async () => {
const eslintErrorSpy = jest.fn();
await commitProject(
serverProcessManager,
projectPath,
projectName,
eslintErrorSpy
);
expect(eslintErrorSpy).not.toBeCalled();
}, 10e3);
});
describe('when starting the server', () => {
let serverProcessManager: ProcessManager;
beforeEach(() => {
serverProcessManager = new ProcessManager();
});
afterEach(async () => {
await serverProcessManager.killAllProcesses();
}, 30e3);
it(
'should not have any ESLint warning or error',
async () => {
const serverTerminal = await startApplication(
serverProcessManager,
'vue-server-eslint'
);
const eslintErrorSpy = jest.fn();
await serverTerminal
.when(/✖ \d+ problems \(\d+ errors, \d+ warnings\)/)
.on('stdout')
.do(eslintErrorSpy)
.until(waitForAppRunning(serverTerminal));
expect(eslintErrorSpy).not.toBeCalled();
},
5 * 60e3
);
});
describe('when the .env file is missing', () => {
let serverProcessManager: ProcessManager;
beforeAll(async () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
deactivateEnvironmentFile(projectPath);
});
afterAll(async () => {
await serverProcessManager.killAllProcesses();
restoreEnvironmentFile(projectPath);
}, 30e3);
it(
'should not start the application',
async () => {
const missingEnvErrorSpy = jest.fn();
const appTerminal = await startApplication(
serverProcessManager,
'vue-server-missing-env'
);
await appTerminal
.when(/\.env file not found in the project root/)
.on('stderr')
.do(missingEnvErrorSpy)
.once();
expect(missingEnvErrorSpy).toHaveBeenCalled();
},
2 * 60e3
);
});
describe('when required environment variables are not defined', () => {
let serverProcessManager: ProcessManager;
let envFileContent = '';
beforeAll(async () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
envFileContent = flushEnvFile(projectPath);
const appTerminal = await startApplication(
serverProcessManager,
'vue-server-invalid'
);
await waitForAppRunning(appTerminal);
[clientPort, serverPort] = getAllocatedPorts();
}, 2 * 60e3);
afterAll(async () => {
overwriteEnvFile(projectPath, envFileContent);
await serverProcessManager.killAllProcesses();
}, 30e3);
it('should redirect the user to an error page', async () => {
await page.goto(searchPageEndpoint(), {waitUntil: 'networkidle2'});
expect(page.url()).toEqual(`${searchPageEndpoint()}/error`);
}, 60e3);
});
describe('when the ports are manually specified', () => {
let serverProcessManager: ProcessManager;
let hardCodedClientPort: number;
let hardCodedServerPort: number;
beforeAll(async () => {
hardCodedClientPort = await getPort();
hardCodedServerPort = await getPort();
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
forceApplicationPorts(hardCodedClientPort, hardCodedServerPort);
const appTerminal = await startApplication(
serverProcessManager,
'vue-server-port-test'
);
await waitForAppRunning(appTerminal);
[clientPort, serverPort] = getAllocatedPorts();
}, 2 * 60e3);
afterAll(async () => {
await serverProcessManager.killAllProcesses();
}, 30e3);
it('should run the application on the specified port', async () => {
expect(clientPort).toEqual(hardCodedClientPort);
}, 60e3);
it('should run the token server on the specified port', async () => {
expect(serverPort).toEqual(hardCodedServerPort);
}, 60e3);
});
describe('when the ports are busy', () => {
const dummyServers: DummyServer[] = [];
let serverProcessManager: ProcessManager;
let usedClientPort: number;
let usedServerPort: number;
beforeAll(async () => {
usedClientPort = await getPort();
usedServerPort = await getPort();
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
forceApplicationPorts(usedClientPort, usedServerPort);
dummyServers.push(
new DummyServer(usedClientPort),
new DummyServer(usedServerPort)
);
const appTerminal = await startApplication(
serverProcessManager,
'vue-server-port-test'
);
await waitForAppRunning(appTerminal);
[clientPort, serverPort] = getAllocatedPorts();
}, 2 * 60e3);
afterAll(async () => {
await Promise.all(dummyServers.map((server) => server.close()));
await serverProcessManager.killAllProcesses();
}, 30e3);
it('should allocate a new port for the application', async () => {
expect(clientPort).not.toEqual(usedClientPort);
});
it('should not use an undefined port for application', async () => {
expect(clientPort).not.toBeUndefined();
});
it('should allocate a new port for the token server', async () => {
expect(serverPort).not.toEqual(usedServerPort);
});
it('should not use an undefined port for token server', async () => {
expect(serverPort).not.toBeUndefined();
});
it('should run the application on a new port', async () => {
await expect(
page.goto(searchPageEndpoint(), {waitUntil: 'load'})
).resolves.not.toThrow();
});
it('should run the server on a new port', async () => {
const tokenRequest = await axios.get(tokenServerEndpoint());
expect(tokenRequest.data.token).toMatch(jwtTokenPattern);
});
});
it.todo('should create a Vue.js project with a custom preset');
});