Skip to content

Commit

Permalink
update config
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Jan 10, 2024
1 parent f9e72af commit 165ef88
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
61 changes: 61 additions & 0 deletions test/browser_new/browser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import fs from 'fs';
import http from 'http';
import path from 'path';
import url from 'url';
import puppeteer from 'puppeteer';

describe('Test browser Parser in the node env', function() {
let server: http.Server;
let browser: puppeteer.Browser;
let page: puppeteer.Page;

beforeAll(async function() {
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const htmlPath = path.resolve(__dirname, 'sample-page.html');
const parserScript = path.resolve(__dirname, '../../browser_new/index.js');

console.info('start server');
server = http.createServer((req, res) => {
res.writeHead(200, { 'content-type': 'text/html' });
if (req.url === '/') {
return fs.createReadStream(htmlPath).pipe(res);
} else if (req.url === '/parser.js') {
return fs.createReadStream(parserScript).pipe(res);
}
});
server.listen(8080);

//use this in case you want to troubleshoot in a real chrome window => browser = await puppeteer.launch({headless: false});
console.info('starting browser');
browser = await puppeteer.launch();

console.info('opening new page');
page = await browser.newPage();

page.on('console', msg => {
msg.args().forEach((arg, index) => {
console.error(`Browser console content ${index}: ${JSON.stringify(arg.remoteObject().value, null, 2)}`);
});
});

console.info('navigating to localhost');
await page.goto('http://localhost:8080', { waitUntil: 'networkidle0' });
});

afterAll(async function() {
await browser.close();
server.close();
});

it('should parse spec in the browser', async function() {
console.info('getting content element');
const contentDiv = await page.$('#content');
const content = await page.evaluate(element => element && element.textContent, contentDiv);
expect(content).toEqual('2.0.0');

console.info('getting number of warnings');
const diagnosticsDiv = await page.$('#diagnostics');
const diagnostics = await page.evaluate(element => element && element.textContent, diagnosticsDiv);
expect(Number(diagnostics)).toBeGreaterThanOrEqual(0);
}, 5000);
});
29 changes: 29 additions & 0 deletions test/browser_new/sample-page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sample page</title>
<meta charset="utf-8" />
</head>

<body>
<div id="content"></div>
<div id="diagnostics"></div>
</body>

<script src="./parser.js"></script>
<script defer>
async function parse() {
try {
const parser = new window.AsyncAPIParser.Parser();
const spec = '{ "asyncapi": "2.0.0", "info": { "title": "My API", "version": "1.0.0" }, "channels": { "/test/tester": { "subscribe": { "operationId": "subscribeOperation", "message": { } } } } }';
const { document: parsedDocument, diagnostics } = await parser.parse(spec);

document.getElementById('content').innerHTML = parsedDocument.version();
document.getElementById('diagnostics').innerHTML = String(diagnostics.length);
} catch (error) {
console.error(error)
}
}
parse();
</script>
</html>
48 changes: 48 additions & 0 deletions webpack_new.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-disable */
const path = require('path');

module.exports = {
entry: './src/index.ts',
target: 'web',
mode: 'production',

output: {
path: path.resolve(__dirname, 'browser_new'),
filename: 'index.js',
globalObject: '(typeof self !== \'undefined\' ? self : this)',
library: {
name: 'AsyncAPIParser',
type: 'umd'
},
},

module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
configFile: 'tsconfig.json',
transpileOnly: true,
},
},
],
},
resolve: {
extensions: ['.ts', '.tsx', '.js'],
fallback: {
fs: false,
path: false,
util: false,
buffer: require.resolve('buffer/'),
}
},

plugins: [
/**
* Uncomment plugin when you wanna see dependency map of bundled package
*/
// (require('webpack-bundle-analyzer').BundleAnalyzerPlugin()),
],
};

0 comments on commit 165ef88

Please sign in to comment.