Skip to content

Commit

Permalink
test: Refactor to async functions (#11832)
Browse files Browse the repository at this point in the history
Co-authored-by: SleepWithCoffee <>
  • Loading branch information
sleepwithcoffee committed Mar 20, 2023
1 parent af2cf61 commit 5b87661
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
36 changes: 18 additions & 18 deletions test/unit/lib/utils/download-template-from-repo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('downloadTemplateFromRepo', () => {
const downloadTemplateFromRepoModule = proxyquire(
'../../../../lib/utils/download-template-from-repo',
{
'node-fetch': (url) => {
'node-fetch': async (url) => {
if (url.indexOf('mybitbucket.server.ltd') > -1) {
return fetchStub();
}
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('downloadTemplateFromRepo', () => {
).to.be.eventually.rejected.and.have.property('code', 'TARGET_FOLDER_ALREADY_EXISTS');
});

it('should download the service based on a regular .git URL', () => {
it('should download the service based on a regular .git URL', async () => {
const url = 'https://example.com/sample-service.git';

return expect(downloadTemplateFromRepo(url)).to.be.fulfilled.then(() => {
Expand All @@ -105,7 +105,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should download and rename the service based on a regular .git URL', () => {
it('should download and rename the service based on a regular .git URL', async () => {
const url = 'https://example.com/sample-service.git';
const name = 'new-service-name';

Expand All @@ -129,7 +129,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should download the service based on a regular .git URL start with git@', () => {
it('should download the service based on a regular .git URL start with git@', async () => {
const url = 'git@example.com/sample-service.git';

return expect(downloadTemplateFromRepo(url)).to.be.fulfilled.then(() => {
Expand All @@ -141,7 +141,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should download and rename the service based on a regular .git URL start with git@', () => {
it('should download and rename the service based on a regular .git URL start with git@', async () => {
const url = 'git@example.com/sample-service.git';
const name = 'new-service-name';

Expand All @@ -165,7 +165,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should download the service based on the GitHub URL', () => {
it('should download the service based on the GitHub URL', async () => {
const url = 'https://github.com/johndoe/service-to-be-downloaded';

return expect(downloadTemplateFromRepo(url)).to.be.fulfilled.then(() => {
Expand All @@ -174,7 +174,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should download and rename the service based on the GitHub URL', () => {
it('should download and rename the service based on the GitHub URL', async () => {
const url = 'https://github.com/johndoe/service-to-be-downloaded';
const name = 'new-service-name';

Expand All @@ -196,7 +196,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should download and rename the service based directories in the GitHub URL', () => {
it('should download and rename the service based directories in the GitHub URL', async () => {
const url = 'https://github.com/serverless/examples/tree/master/rest-api-with-dynamodb';
const name = 'new-service-name';

Expand Down Expand Up @@ -252,7 +252,7 @@ describe('downloadTemplateFromRepo', () => {
).to.be.eventually.rejected.and.have.property('code', 'INVALID_TEMPLATE_PROVIDER');
});

it('should parse a valid GitHub URL', () => {
it('should parse a valid GitHub URL', async () => {
return expect(parseRepoURL('https://github.com/serverless/serverless')).to.be.fulfilled.then(
(output) => {
expect(output).to.deep.eq({
Expand All @@ -269,7 +269,7 @@ describe('downloadTemplateFromRepo', () => {
);
});

it('should parse a valid GitHub URL with subdirectory', () => {
it('should parse a valid GitHub URL with subdirectory', async () => {
return expect(
parseRepoURL('https://github.com/serverless/serverless/tree/master/assets')
).to.be.fulfilled.then((output) => {
Expand All @@ -286,7 +286,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should parse a valid GitHub Entreprise URL', () => {
it('should parse a valid GitHub Entreprise URL', async () => {
return expect(
parseRepoURL('https://github.mydomain.com/serverless/serverless')
).to.be.fulfilled.then((output) => {
Expand All @@ -303,7 +303,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should parse a valid GitHub Entreprise with subdirectory', () => {
it('should parse a valid GitHub Entreprise with subdirectory', async () => {
return expect(
parseRepoURL('https://github.mydomain.com/serverless/serverless/tree/master/assets')
).to.be.fulfilled.then((output) => {
Expand All @@ -320,7 +320,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should parse a valid GitHub Entreprise URL with authentication', () => {
it('should parse a valid GitHub Entreprise URL with authentication', async () => {
return expect(
parseRepoURL('https://username:password@github.com/serverless/serverless/')
).to.be.fulfilled.then((output) => {
Expand All @@ -337,7 +337,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should parse a valid BitBucket URL', () => {
it('should parse a valid BitBucket URL', async () => {
return parseRepoURL('https://bitbucket.org/atlassian/localstack').then((output) => {
expect(output).to.deep.eq({
owner: 'atlassian',
Expand All @@ -352,7 +352,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should parse a valid BitBucket URL with subdirectory', () => {
it('should parse a valid BitBucket URL with subdirectory', async () => {
return parseRepoURL(
'https://bitbucket.org/atlassian/localstack/src/85870856fd6941ae75c0fa946a51cf756ff2f53a/localstack/dashboard/?at=mvn'
).then((output) => {
Expand All @@ -369,7 +369,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should parse a valid Bitbucket Server URL', () => {
it('should parse a valid Bitbucket Server URL', async () => {
return parseRepoURL(
'https://user:pass@mybitbucket.server.ltd/rest/api/latest/projects/myproject/repos/myrepo/archive?at=refs%2Fheads%2Fdevelop'
).then((output) => {
Expand All @@ -387,7 +387,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should parse a valid GitLab URL ', () => {
it('should parse a valid GitLab URL ', async () => {
return parseRepoURL('https://gitlab.com/serverless/serverless').then((output) => {
expect(output).to.deep.eq({
owner: 'serverless',
Expand All @@ -403,7 +403,7 @@ describe('downloadTemplateFromRepo', () => {
});
});

it('should parse a valid GitLab URL with subdirectory', () => {
it('should parse a valid GitLab URL with subdirectory', async () => {
return parseRepoURL('https://gitlab.com/serverless/serverless/tree/dev/subdir').then(
(output) => {
expect(output).to.deep.eq({
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/utils/fs/create-zip-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ chai.use(require('sinon-chai'));
const expect = require('chai').expect;

describe('#createZipFile()', () => {
it('should create a zip file with the source directory content', () => {
it('should create a zip file with the source directory content', async () => {
const toZipFilePath = createTmpFile('foo.json');
const zipFilePath = createTmpFile('package.zip');

Expand Down
8 changes: 4 additions & 4 deletions test/unit/lib/utils/fs/read-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ chai.use(require('sinon-chai'));
const expect = require('chai').expect;

describe('#readFile()', () => {
it('should read a file asynchronously', () => {
it('should read a file asynchronously', async () => {
const tmpFilePath = getTmpFilePath('anything.json');

return writeFile(tmpFilePath, { foo: 'bar' }).then(() =>
expect(readFile(tmpFilePath)).to.eventually.deep.equal({ foo: 'bar' })
);
});

it('should read a filename extension .yml', () => {
it('should read a filename extension .yml', async () => {
const tmpFilePath = getTmpFilePath('anything.yml');

return writeFile(tmpFilePath, { foo: 'bar' }).then(() =>
expect(readFile(tmpFilePath)).to.eventually.deep.equal({ foo: 'bar' })
);
});

it('should read a filename extension .yaml', () => {
it('should read a filename extension .yaml', async () => {
const tmpFilePath = getTmpFilePath('anything.yaml');

return writeFile(tmpFilePath, { foo: 'bar' }).then(() =>
expect(readFile(tmpFilePath)).to.eventually.deep.equal({ foo: 'bar' })
);
});

it('should throw YAMLException with filename if yml file is invalid format', () => {
it('should throw YAMLException with filename if yml file is invalid format', async () => {
const tmpFilePath = getTmpFilePath('invalid.yml');
return writeFile(tmpFilePath, ': a')
.then(() => readFile(tmpFilePath))
Expand Down
6 changes: 3 additions & 3 deletions test/unit/lib/utils/fs/write-file-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('#writeFileSync()', () => {
expect(obj.foo).to.equal('bar');
});

it('should write a .yml file synchronously', () => {
it('should write a .yml file synchronously', async () => {
const tmpFilePath = getTmpFilePath('anything.yml');

writeFileSync(tmpFilePath, { foo: 'bar' });
Expand All @@ -33,7 +33,7 @@ describe('#writeFileSync()', () => {
});
});

it('should write a .yaml file synchronously', () => {
it('should write a .yaml file synchronously', async () => {
const tmpFilePath = getTmpFilePath('anything.yaml');

writeFileSync(tmpFilePath, { foo: 'bar' });
Expand All @@ -49,7 +49,7 @@ describe('#writeFileSync()', () => {
}).to.throw(Error);
});

it('should be able to write an object with circular references', () => {
it('should be able to write an object with circular references', async () => {
const tmpFilePath = getTmpFilePath('anything.json');
const bar = {};
bar.foo = bar;
Expand Down
8 changes: 4 additions & 4 deletions test/unit/lib/utils/fs/write-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@ describe('#writeFile()', function () {
serverless = new Serverless({ commands: [], options: {} });
});

it('should write a .json file asynchronously', () => {
it('should write a .json file asynchronously', async () => {
const tmpFilePath = getTmpFilePath('anything.json');
return writeFile(tmpFilePath, { foo: 'bar' }).then(() =>
expect(readFile(tmpFilePath)).to.eventually.deep.equal({ foo: 'bar' })
);
});

it('should write a .yml file synchronously', () => {
it('should write a .yml file synchronously', async () => {
const tmpFilePath = getTmpFilePath('anything.yml');

return writeFile(tmpFilePath, { foo: 'bar' }).then(() =>
expect(serverless.yamlParser.parse(tmpFilePath)).to.eventually.deep.equal({ foo: 'bar' })
);
});

it('should write a .yaml file synchronously', () => {
it('should write a .yaml file synchronously', async () => {
const tmpFilePath = getTmpFilePath('anything.yaml');

return writeFile(tmpFilePath, { foo: 'bar' }).then(() =>
expect(serverless.yamlParser.parse(tmpFilePath)).to.eventually.deep.equal({ foo: 'bar' })
);
});

it('should be able to write an object with circular references', () => {
it('should be able to write an object with circular references', async () => {
const tmpFilePath = getTmpFilePath('anything.json');
const bar = {};
bar.foo = bar;
Expand Down
9 changes: 7 additions & 2 deletions test/unit/lib/utils/yaml-ast-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ describe('#yamlAstParser', () => {
});

describe('#addNewArrayItem()', () => {
const addNewArrayItemAndVerifyResult = (yamlContent, pathInYaml, newItem, expectedResult) => {
const addNewArrayItemAndVerifyResult = async (
yamlContent,
pathInYaml,
newItem,
expectedResult
) => {
const yamlFilePath = path.join(tmpDirPath, 'test.yaml');
writeFileSync(yamlFilePath, yamlContent);
return expect(
Expand Down Expand Up @@ -169,7 +174,7 @@ describe('#yamlAstParser', () => {
});

describe('#removeExistingArrayItem()', () => {
const removeExistingArrayItemAndVerifyResult = (
const removeExistingArrayItemAndVerifyResult = async (
yamlContent,
pathInYaml,
removeItem,
Expand Down

0 comments on commit 5b87661

Please sign in to comment.