{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "182aef1d", "metadata": {}, "source": [ "# Integration Tests for Deno Jupyter\n", "This notebook contains a number of tests to ensure that Jupyter is working as expected. You should be able to select \"Kernel -> Restart Kernel and Run All\" in Jupyter's notebook UI to run the tests." ] }, { "attachments": {}, "cell_type": "markdown", "id": "d7705d88", "metadata": {}, "source": [ "## Passing Tests" ] }, { "attachments": {}, "cell_type": "markdown", "id": "669f972e", "metadata": { "heading_collapsed": true }, "source": [ "### Simple Tests" ] }, { "attachments": {}, "cell_type": "markdown", "id": "e7e8a512", "metadata": { "hidden": true }, "source": [ "#### This test should print \"hi\".\n", "If this doesn't work, everything else will probably fail :)" ] }, { "cell_type": "code", "execution_count": 21, "id": "a5d38758", "metadata": { "hidden": true }, "outputs": [ { "data": {}, "execution_count": 21, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "hi\n" ] } ], "source": [ "console.log(\"hi\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "bc5ce8e3", "metadata": { "hidden": true }, "source": [ "#### Top-level await" ] }, { "cell_type": "code", "execution_count": 22, "id": "f7fa885a", "metadata": { "hidden": true }, "outputs": [ { "data": {}, "execution_count": 22, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "x is \u001b[33m42\u001b[39m\n" ] } ], "source": [ "let x = await Promise.resolve(42);\n", "console.log(\"x is\", x);" ] }, { "attachments": {}, "cell_type": "markdown", "id": "c21455ae", "metadata": { "hidden": true }, "source": [ "#### TypeScript transpiling\n", "Credit to [typescriptlang.org](https://www.typescriptlang.org/docs/handbook/interfaces.html) for this code" ] }, { "cell_type": "code", "execution_count": 23, "id": "08a17340", "metadata": { "hidden": true }, "outputs": [ { "data": { "text/plain": [ "{ color: \u001b[32m\"red\"\u001b[39m, area: \u001b[33m10000\u001b[39m }" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "interface SquareConfig {\n", " color?: string;\n", " width?: number;\n", "}\n", " \n", "function createSquare(config: SquareConfig): { color: string; area: number } {\n", " return {\n", " color: config.color || \"red\",\n", " area: config.width ? config.width * config.width : 20,\n", " };\n", "}\n", " \n", "createSquare({ colour: \"red\", width: 100 });" ] }, { "attachments": {}, "cell_type": "markdown", "id": "eaa0ebc0", "metadata": { "heading_collapsed": true }, "source": [ "### Return Values" ] }, { "attachments": {}, "cell_type": "markdown", "id": "52876276", "metadata": { "hidden": true }, "source": [ "#### undefined should not return a value" ] }, { "cell_type": "code", "execution_count": 24, "id": "bbf2c09b", "metadata": { "hidden": true }, "outputs": [ { "data": {}, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "undefined" ] }, { "attachments": {}, "cell_type": "markdown", "id": "e175c803", "metadata": { "hidden": true }, "source": [ "#### null should return \"null\"" ] }, { "cell_type": "code", "execution_count": 25, "id": "d9801d80", "metadata": { "hidden": true }, "outputs": [ { "data": { "text/plain": [ "\u001b[1mnull\u001b[22m" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "null" ] }, { "attachments": {}, "cell_type": "markdown", "id": "a2a716dc", "metadata": { "hidden": true }, "source": [ "#### boolean should return the boolean" ] }, { "cell_type": "code", "execution_count": 26, "id": "cfaac330", "metadata": { "hidden": true }, "outputs": [ { "data": { "text/plain": [ "\u001b[33mtrue\u001b[39m" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "true" ] }, { "attachments": {}, "cell_type": "markdown", "id": "8d9f1aba", "metadata": { "hidden": true }, "source": [ "#### number should return the number" ] }, { "cell_type": "code", "execution_count": 27, "id": "ec3be2da", "metadata": { "hidden": true }, "outputs": [ { "data": { "text/plain": [ "\u001b[33m42\u001b[39m" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "42" ] }, { "attachments": {}, "cell_type": "markdown", "id": "60965915", "metadata": { "hidden": true }, "source": [ "#### string should return the string" ] }, { "cell_type": "code", "execution_count": 28, "id": "997cf2d7", "metadata": { "hidden": true }, "outputs": [ { "data": { "text/plain": [ "\u001b[32m\"this is a test of the emergency broadcast system\"\u001b[39m" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"this is a test of the emergency broadcast system\"" ] }, { "attachments": {}, "cell_type": "markdown", "id": "fe38dc27", "metadata": { "hidden": true }, "source": [ "#### bigint should return the bigint in literal format" ] }, { "cell_type": "code", "execution_count": 29, "id": "44b63807", "metadata": { "hidden": true }, "outputs": [ { "data": { "text/plain": [ "\u001b[33m31337n\u001b[39m" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "31337n" ] }, { "attachments": {}, "cell_type": "markdown", "id": "843ccb6c", "metadata": { "hidden": true }, "source": [ "#### symbol should return a string describing the symbol" ] }, { "cell_type": "code", "execution_count": 30, "id": "e10c0d31", "metadata": { "hidden": true }, "outputs": [ { "data": { "text/plain": [ "\u001b[32mSymbol(foo)\u001b[39m" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Symbol(\"foo\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "171b817f", "metadata": { "hidden": true }, "source": [ "#### object should describe the object inspection" ] }, { "cell_type": "code", "execution_count": 31, "id": "81c99233", "metadata": { "hidden": true }, "outputs": [ { "data": { "text/plain": [ "{ foo: \u001b[32m\"bar\"\u001b[39m }" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{foo: \"bar\"}" ] }, { "attachments": {}, "cell_type": "markdown", "id": "6caeb583", "metadata": { "hidden": true }, "source": [ "#### resolve returned promise" ] }, { "cell_type": "code", "execution_count": 32, "id": "43c1581b", "metadata": { "hidden": true }, "outputs": [ { "data": { "text/plain": [ "Promise { \u001b[32m\"it worked!\"\u001b[39m }" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Promise.resolve(\"it worked!\")" ] }, { "cell_type": "code", "execution_count": 33, "id": "9a34b725", "metadata": { "hidden": true }, "outputs": [ { "data": { "text/plain": [ "Promise {\n", " \u001b[36m\u001b[39m Error: it failed!\n", " at :2:16\n", "}" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Promise.reject(new Error(\"it failed!\"));" ] }, { "cell_type": "code", "execution_count": 34, "id": "b5c7b819", "metadata": { "scrolled": true }, "outputs": [ { "ename": "Error", "evalue": "this is a test", "output_type": "error", "traceback": [ "Stack trace:", "Error: this is a test", " at foo (:3:9)", " at :4:3" ] } ], "source": [ "(function foo() {\n", " throw new Error(\"this is a test\")\n", "})()" ] }, { "cell_type": "code", "execution_count": 35, "id": "14844fc9-536e-4121-a9bd-fc2d3f7b6395", "metadata": {}, "outputs": [ { "ename": "Unknown error", "evalue": "a party", "output_type": "error", "traceback": [ "Stack trace:", "\"a party\"", " at " ] } ], "source": [ "throw \"a party\"" ] }, { "cell_type": "code", "execution_count": 36, "id": "72d01fdd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Promise {\n", " \u001b[36m\u001b[39m TypeError: Expected string at position 1\n", " at Object.readFile (ext:deno_fs/30_fs.js:716:29)\n", " at :2:6\n", "}" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Deno.readFile(1)" ] }, { "cell_type": "code", "execution_count": 37, "id": "28cf59d0-6908-4edc-bb10-c325beeee362", "metadata": {}, "outputs": [ { "data": {}, "execution_count": 37, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "Hello from Deno!\n" ] } ], "source": [ "console.log(\"Hello from Deno!\")" ] }, { "cell_type": "code", "execution_count": 38, "id": "8d5485c3-0da3-43fe-8ef5-a61e672f5e81", "metadata": {}, "outputs": [ { "data": {}, "execution_count": 38, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[48;2;21;128;61m\u001b[37m Hello Deno \u001b[0m\n" ] } ], "source": [ "console.log(\"%c Hello Deno \", \"background-color: #15803d; color: white;\");" ] }, { "cell_type": "code", "execution_count": 39, "id": "1401d9d5-6994-4c7b-b55a-db3c16a1e2dc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[32m\"Cool 🫡\"\u001b[39m" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Cool 🫡\"" ] }, { "cell_type": "code", "execution_count": 40, "id": "7afdaa0a-a2a0-4f52-8c7d-b6c5f237aa0d", "metadata": {}, "outputs": [ { "data": {}, "execution_count": 40, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "┌───────┬────────┐\n", "│ (idx) │ Values │\n", "├───────┼────────┤\n", "│ 0 │ 1 │\n", "│ 1 │ 2 │\n", "│ 2 │ 3 │\n", "└───────┴────────┘\n" ] } ], "source": [ "console.table([1, 2, 3])" ] }, { "cell_type": "code", "execution_count": 1, "id": "8e93df23-06eb-414b-98d4-51fbebb53d1f", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "Cannot read properties of undefined (reading 'broadcast')", "output_type": "error", "traceback": [ "Stack trace:", "TypeError: Cannot read properties of undefined (reading 'broadcast')", " at :2:20" ] } ], "source": [ "await Deno.jupyter.broadcast(\"display_data\", {\n", " data: { \"text/html\": \"Processing.\" },\n", " metadata: {},\n", " transient: { display_id: \"progress\" }\n", "});\n", "\n", "await new Promise((resolve) => setTimeout(resolve, 500));\n", "\n", "await Deno.jupyter.broadcast(\"update_display_data\", {\n", " data: { \"text/html\": \"Processing..\" },\n", " metadata: {},\n", " transient: { display_id: \"progress\" }\n", "});\n", "\n", "await new Promise((resolve) => setTimeout(resolve, 500));\n", "\n", "await Deno.jupyter.broadcast(\"update_display_data\", {\n", " data: { \"text/html\": \"Processing...\" },\n", " metadata: {},\n", " transient: { display_id: \"progress\" }\n", "});\n", "\n", "await new Promise((resolve) => setTimeout(resolve, 500));\n", "\n", "await Deno.jupyter.broadcast(\"update_display_data\", {\n", " data: { \"text/html\": \"Complete ✅\" },\n", " metadata: {},\n", " transient: { display_id: \"progress\" }\n", "});" ] }, { "cell_type": "code", "execution_count": null, "id": "0181f28e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Deno", "language": "typescript", "name": "deno" }, "language_info": { "file_extension": ".ts", "mimetype": "text/x.typescript", "name": "typescript", "nb_converter": "script", "pygments_lexer": "typescript", "version": "5.2.2" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 5 }